import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
import cv2
import os
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
from tensorflow.keras.layers import ZeroPadding2D, Convolution2D, Activation
from tensorflow.keras.applications.mobilenet import preprocess_input
from tensorflow.keras.applications.mobilenet import MobileNet
from tensorflow.keras.models import Model
from tensorflow import expand_dims
from sklearn.metrics import classification_report
from glob import glob
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import precision_recall_curve,accuracy_score,f1_score,precision_score,recall_score
# load the drive from google
from zipfile import ZipFile
from google.colab import drive
drive.mount('/content/drive')
# extract the images
images_path = "/content/drive/MyDrive/Project Files/Car+Images.zip"
with ZipFile(images_path,'r') as zip1:
zip1.extractall()
# extract the images
annotations_path = "/content/drive/MyDrive/Project Files/Annotations.zip"
with ZipFile(annotations_path,'r') as zip2:
zip2.extractall()
Mounted at /content/drive
# method to load the images and its corresponding annotations
def loadImages(imagesPath):
images = glob(imagesPath)
img_list = []
images_list_iter=iter(images)
for i in range(len(images)):
img=next(images_list_iter)
images_dict={}
#images_dict["car_orig_image"]=cv2.imread(img,cv2.IMREAD_GRAYSCALE)
images_dict["car_orig_image"]=cv2.imread(img)
images_dict["car_name"]=str(img.split('/')[-2])
images_dict["car_image_name"]=str(img.split('/')[-1])
img_list.append(images_dict)
return img_list
## Reading all images in Train folder
train_images= loadImages('Car Images/Train Images/*/*.jpg')
train_images[0]
{'car_orig_image': array([[[112, 116, 104],
[108, 113, 104],
[105, 114, 111],
...,
[204, 199, 200],
[247, 245, 245],
[255, 254, 254]],
[[108, 112, 100],
[102, 110, 100],
[104, 112, 111],
...,
[204, 199, 200],
[247, 245, 245],
[255, 254, 254]],
[[110, 116, 105],
[105, 113, 103],
[109, 117, 116],
...,
[204, 199, 200],
[247, 245, 245],
[255, 254, 254]],
...,
[[142, 145, 149],
[138, 141, 145],
[135, 138, 142],
...,
[136, 136, 136],
[231, 231, 231],
[255, 255, 255]],
[[145, 148, 152],
[142, 145, 149],
[138, 141, 145],
...,
[134, 134, 134],
[231, 231, 231],
[255, 255, 255]],
[[150, 153, 157],
[146, 149, 153],
[143, 146, 150],
...,
[133, 133, 133],
[231, 231, 231],
[255, 255, 255]]], dtype=uint8),
'car_name': 'Audi R8 Coupe 2012',
'car_image_name': '00450.jpg'}
print("Number of images read in train set : {}".format(len(train_images)))
Number of images read in train set : 8144
## Reading all images in Train folder
test_images= loadImages('Car Images/Test Images/*/*.jpg')
test_images[0]
{'car_orig_image': array([[[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1],
...,
[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1]],
[[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1],
...,
[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1]],
[[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1],
...,
[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1]],
...,
[[16, 16, 16],
[16, 16, 16],
[16, 16, 16],
...,
[16, 16, 16],
[16, 16, 16],
[16, 16, 16]],
[[16, 16, 16],
[16, 16, 16],
[16, 16, 16],
...,
[18, 18, 18],
[17, 17, 17],
[17, 17, 17]],
[[16, 16, 16],
[16, 16, 16],
[16, 16, 16],
...,
[18, 18, 18],
[17, 17, 17],
[17, 17, 17]]], dtype=uint8),
'car_name': 'Audi R8 Coupe 2012',
'car_image_name': '08029.jpg'}
print("Number of images read in test set : {}".format(len(test_images)))
Number of images read in test set : 8041
Note: Assigning classes and anaotations in separate steps will result in memory and compute waste. So the below function addresses both the assignments in one go.
def loadAnnot_map(annotPath,images_list):
images_list_iter=iter(images_list)
annots_df=pd.read_csv(annotPath)
annots_df.rename(columns={'Image Name':'car_image_name', 'Bounding Box coordinates':'x1', 'Unnamed: 2':'y1','Unnamed: 3': 'x2', 'Unnamed: 4': 'y2', 'Image class': 'image_class'},inplace=True)
annots_df["bounding_box"]=list(annots_df[['x1', 'y1', 'x2', 'y2']].to_records(index=False))
annots_dict = dict([(i,[x,y]) for i, x,y in zip(annots_df.car_image_name, annots_df.image_class,annots_df.bounding_box)])
imglist_upd=[]
for i in range(len(images_list)):
img_dict=next(images_list_iter)
car_img_nm=img_dict["car_image_name"]
img_dict["car_image_class"]=annots_dict[car_img_nm][0]
img_dict["bounding_box"]=annots_dict[car_img_nm][1]
imglist_upd.append(img_dict)
return imglist_upd
## Reading annotations for train set and mapping to train images
train_images_upd= loadAnnot_map('Annotations/Train Annotations.csv',train_images)
## Sample record
train_images_upd[0]
{'car_orig_image': array([[[112, 116, 104],
[108, 113, 104],
[105, 114, 111],
...,
[204, 199, 200],
[247, 245, 245],
[255, 254, 254]],
[[108, 112, 100],
[102, 110, 100],
[104, 112, 111],
...,
[204, 199, 200],
[247, 245, 245],
[255, 254, 254]],
[[110, 116, 105],
[105, 113, 103],
[109, 117, 116],
...,
[204, 199, 200],
[247, 245, 245],
[255, 254, 254]],
...,
[[142, 145, 149],
[138, 141, 145],
[135, 138, 142],
...,
[136, 136, 136],
[231, 231, 231],
[255, 255, 255]],
[[145, 148, 152],
[142, 145, 149],
[138, 141, 145],
...,
[134, 134, 134],
[231, 231, 231],
[255, 255, 255]],
[[150, 153, 157],
[146, 149, 153],
[143, 146, 150],
...,
[133, 133, 133],
[231, 231, 231],
[255, 255, 255]]], dtype=uint8),
'car_name': 'Audi R8 Coupe 2012',
'car_image_name': '00450.jpg',
'car_image_class': 15,
'bounding_box': (57, 59, 506, 288)}
print("Number of images in updated train image list : {}".format(len(train_images_upd)))
Number of images in updated train image list : 8144
del train_images
## Let us explore the train set
train_cars=pd.DataFrame([{'car_name': x['car_name'], 'image_size': x['car_orig_image'].shape, 'car_image_class':x['car_image_class']} for x in train_images_upd])
cars_uniq= train_cars[['car_image_class','car_name']]
cars_uniq=cars_uniq.drop_duplicates()
cars_dict=dict(cars_uniq.values)
cars_dict
{15: 'Audi R8 Coupe 2012',
19: 'Audi TT Hatchback 2011',
186: 'Toyota Sequoia SUV 2012',
119: 'GMC Savana Van 2012',
25: 'Audi TT RS Coupe 2012',
32: 'BMW X5 SUV 2007',
107: 'Ford Mustang Convertible 2007',
106: 'Ford F-450 Super Duty Crew Cab 2012',
188: 'Toyota Corolla Sedan 2012',
94: 'Dodge Durango SUV 2012',
89: 'Dodge Journey SUV 2012',
51: 'Cadillac CTS-V Sedan 2012',
152: 'Lamborghini Gallardo LP 570-4 Superleggera 2012',
104: 'Ferrari 458 Italia Coupe 2012',
6: 'Acura Integra Type R 2001',
91: 'Dodge Dakota Club Cab 2007',
24: 'Audi S4 Sedan 2007',
52: 'Cadillac SRX SUV 2012',
125: 'HUMMER H2 SUT Crew Cab 2009',
49: 'Buick Verano Sedan 2012',
75: 'Chevrolet Silverado 1500 Regular Cab 2012',
138: 'Hyundai Sonata Sedan 2012',
187: 'Toyota Camry Sedan 2012',
13: 'Audi A5 Coupe 2012',
142: 'Infiniti QX56 SUV 2011',
29: 'BMW 3 Series Sedan 2012',
126: 'Honda Odyssey Minivan 2012',
150: 'Lamborghini Reventon Coupe 2008',
124: 'HUMMER H3T Crew Cab 2010',
122: 'GMC Canyon Extended Cab 2012',
61: 'Chevrolet Impala Sedan 2007',
183: 'Suzuki SX4 Hatchback 2012',
27: 'BMW 1 Series Convertible 2012',
4: 'Acura TL Type-S 2008',
3: 'Acura TL Sedan 2012',
174: 'Ram C-V Cargo Van Minivan 2012',
34: 'BMW M3 Coupe 2012',
108: 'Ford Freestar Minivan 2007',
196: 'smart fortwo Convertible 2012',
121: 'GMC Acadia SUV 2012',
101: 'Ferrari FF Coupe 2012',
135: 'Hyundai Elantra Sedan 2007',
73: 'Chevrolet Malibu Sedan 2007',
98: 'Eagle Talon Hatchback 1998',
57: 'Chevrolet Corvette Ron Fellows Edition Z06 2007',
180: 'Spyker C8 Coupe 2009',
154: 'Land Rover Range Rover SUV 2012',
146: 'Jeep Wrangler SUV 2012',
166: 'Mercedes-Benz Sprinter Van 2012',
53: 'Cadillac Escalade EXT Crew Cab 2007',
128: 'Honda Accord Coupe 2012',
102: 'Ferrari California Convertible 2012',
28: 'BMW 1 Series Coupe 2012',
90: 'Dodge Dakota Crew Cab 2010',
140: 'Hyundai Azera Sedan 2012',
40: 'Bentley Arnage Sedan 2009',
84: 'Dodge Caliber Wagon 2007',
112: 'Ford GT Coupe 2006',
96: 'Dodge Charger Sedan 2012',
76: 'Chrysler Aspen SUV 2009',
80: 'Chrysler Crossfire Convertible 2008',
66: 'Chevrolet Cobalt SS 2010',
33: 'BMW X6 SUV 2012',
38: 'BMW Z4 Convertible 2012',
58: 'Chevrolet Traverse SUV 2012',
127: 'Honda Odyssey Minivan 2007',
149: 'Jeep Compass SUV 2012',
117: 'Ford Fiesta Sedan 2012',
18: 'Audi 100 Wagon 1994',
10: 'Aston Martin Virage Convertible 2012',
157: 'MINI Cooper Roadster Convertible 2012',
161: 'Mercedes-Benz 300-Class Convertible 1993',
82: 'Daewoo Nubira Wagon 2002',
110: 'Ford Edge SUV 2012',
64: 'Chevrolet Express Cargo Van 2007',
55: 'Chevrolet Corvette Convertible 2012',
9: 'Aston Martin V8 Vantage Coupe 2012',
85: 'Dodge Caravan Minivan 1997',
26: 'BMW ActiveHybrid 5 Sedan 2012',
63: 'Chevrolet Sonic Sedan 2012',
11: 'Aston Martin Virage Coupe 2012',
181: 'Suzuki Aerio Sedan 2007',
47: 'Buick Regal GS 2012',
46: 'Bugatti Veyron 16.4 Coupe 2009',
176: 'Rolls-Royce Ghost Sedan 2012',
86: 'Dodge Ram Pickup 3500 Crew Cab 2010',
8: 'Aston Martin V8 Vantage Convertible 2012',
137: 'Hyundai Genesis Sedan 2012',
133: 'Hyundai Veracruz SUV 2012',
111: 'Ford Ranger SuperCab 2011',
60: 'Chevrolet HHR SS 2010',
81: 'Chrysler PT Cruiser Convertible 2008',
93: 'Dodge Challenger SRT8 2011',
114: 'Ford F-150 Regular Cab 2007',
113: 'Ford F-150 Regular Cab 2012',
22: 'Audi S5 Coupe 2012',
171: 'Nissan 240SX Coupe 1998',
153: 'Lamborghini Diablo Coupe 2001',
17: 'Audi 100 Sedan 1994',
65: 'Chevrolet Avalanche Crew Cab 2012',
123: 'Geo Metro Convertible 1993',
79: 'Chrysler 300 SRT-8 2010',
62: 'Chevrolet Tahoe Hybrid SUV 2012',
2: 'Acura RL Sedan 2012',
109: 'Ford Expedition EL SUV 2009',
191: 'Volkswagen Golf Hatchback 1991',
67: 'Chevrolet Malibu Hybrid Sedan 2010',
168: 'Nissan Leaf Hatchback 2012',
36: 'BMW M6 Convertible 2010',
165: 'Mercedes-Benz S-Class Sedan 2012',
139: 'Hyundai Elantra Touring Hatchback 2012',
54: 'Chevrolet Silverado 1500 Hybrid Crew Cab 2012',
14: 'Audi TTS Coupe 2012',
162: 'Mercedes-Benz C-Class Sedan 2012',
70: 'Chevrolet Silverado 1500 Classic Extended Cab 2007',
193: 'Volvo C30 Hatchback 2012',
170: 'Nissan Juke Hatchback 2012',
185: 'Tesla Model S Sedan 2012',
131: 'Hyundai Santa Fe SUV 2012',
21: 'Audi S5 Convertible 2012',
118: 'GMC Terrain SUV 2012',
83: 'Dodge Caliber Wagon 2012',
5: 'Acura TSX Sedan 2012',
115: 'Ford Focus Sedan 2007',
88: 'Dodge Sprinter Cargo Van 2009',
116: 'Ford E-Series Wagon Van 2012',
45: 'Bugatti Veyron 16.4 Convertible 2009',
7: 'Acura ZDX Hatchback 2012',
105: 'Fisker Karma Sedan 2012',
169: 'Nissan NV Passenger Van 2012',
87: 'Dodge Ram Pickup 3500 Quad Cab 2009',
192: 'Volkswagen Beetle Hatchback 2012',
173: 'Porsche Panamera Sedan 2012',
189: 'Toyota 4Runner SUV 2012',
182: 'Suzuki Kizashi Sedan 2012',
68: 'Chevrolet TrailBlazer SS 2009',
148: 'Jeep Grand Cherokee SUV 2012',
155: 'Land Rover LR2 SUV 2012',
160: 'McLaren MP4-12C Coupe 2012',
74: 'Chevrolet Silverado 1500 Extended Cab 2012',
41: 'Bentley Mulsanne Sedan 2011',
103: 'Ferrari 458 Italia Convertible 2012',
175: 'Rolls-Royce Phantom Drophead Coupe Convertible 2012',
151: 'Lamborghini Aventador Coupe 2012',
190: 'Volkswagen Golf Hatchback 2012',
163: 'Mercedes-Benz SL-Class Coupe 2009',
16: 'Audi V8 Sedan 1994',
78: 'Chrysler Town and Country Minivan 2012',
95: 'Dodge Durango SUV 2007',
144: 'Jaguar XK XKR 2012',
56: 'Chevrolet Corvette ZR1 2012',
69: 'Chevrolet Silverado 2500HD Regular Cab 2012',
59: 'Chevrolet Camaro Convertible 2012',
141: 'Infiniti G Coupe IPL 2012',
35: 'BMW M5 Sedan 2010',
23: 'Audi S4 Sedan 2012',
42: 'Bentley Continental GT Coupe 2012',
12: 'Audi RS 4 Convertible 2008',
1: 'AM General Hummer SUV 2000',
194: 'Volvo 240 Sedan 1993',
143: 'Isuzu Ascender SUV 2008',
92: 'Dodge Magnum Wagon 2008',
43: 'Bentley Continental GT Coupe 2007',
99: 'FIAT 500 Abarth 2012',
158: 'Maybach Landaulet Convertible 2012',
37: 'BMW X3 SUV 2012',
72: 'Chevrolet Monte Carlo Coupe 2007',
164: 'Mercedes-Benz E-Class Sedan 2012',
172: 'Plymouth Neon Coupe 1999',
130: 'Hyundai Veloster Hatchback 2012',
100: 'FIAT 500 Convertible 2012',
30: 'BMW 3 Series Wagon 2012',
136: 'Hyundai Accent Sedan 2012',
48: 'Buick Rainier SUV 2007',
134: 'Hyundai Sonata Hybrid Sedan 2012',
132: 'Hyundai Tucson SUV 2012',
184: 'Suzuki SX4 Sedan 2012',
178: 'Scion xD Hatchback 2012',
159: 'Mazda Tribute SUV 2011',
145: 'Jeep Patriot SUV 2012',
179: 'Spyker C8 Convertible 2009',
195: 'Volvo XC90 SUV 2007',
77: 'Chrysler Sebring Convertible 2010',
44: 'Bentley Continental Flying Spur Sedan 2007',
167: 'Mitsubishi Lancer Sedan 2012',
156: 'Lincoln Town Car Sedan 2011',
50: 'Buick Enclave SUV 2012',
20: 'Audi S6 Sedan 2011',
71: 'Chevrolet Express Van 2007',
120: 'GMC Yukon Hybrid SUV 2012',
39: 'Bentley Continental Supersports Conv. Convertible 2012',
31: 'BMW 6 Series Convertible 2007',
147: 'Jeep Liberty SUV 2012',
97: 'Dodge Charger SRT-8 2009',
129: 'Honda Accord Sedan 2012',
177: 'Rolls-Royce Phantom Sedan 2012'}
train_cars_cnt=train_cars['car_name'].value_counts(sort=True)
train_cars_cnt_df = pd.DataFrame(train_cars_cnt)
train_cars_cnt_df = train_cars_cnt_df.reset_index()
train_cars_cnt_df.columns = ['car_names', 'counts']
train_cars_cnt_df.sort_values(by=['counts'],ascending=False).head()
| car_names | counts | |
|---|---|---|
| 0 | GMC Savana Van 2012 | 68 |
| 1 | Chrysler 300 SRT-8 2010 | 49 |
| 2 | Mitsubishi Lancer Sedan 2012 | 48 |
| 3 | Mercedes-Benz 300-Class Convertible 1993 | 48 |
| 4 | Jaguar XK XKR 2012 | 47 |
## Plotting by car images count - Top 10
plt.figure(figsize=(8,5))
train_cars_cnt_df.sort_values(by=['counts'],ascending=False)
sns.barplot(data=train_cars_cnt_df.head(10), x='counts',y='car_names')
plt.xticks(rotation=90)
plt.title("Top 10 by Car images count",size=8)
plt.show()
## Plotting by car images count - Bottom 10
plt.figure(figsize=(8,5))
train_cars_cnt_df.sort_values(by=['counts'],ascending=False)
sns.barplot(data=train_cars_cnt_df.tail(10), x='counts',y='car_names')
plt.xticks(rotation=90)
plt.title("Bottom 10 by Car images count",size=8)
plt.show()
## Plotting car images count for each bin
plt.figure(figsize=(5,5))
train_cars_cnt_df.sort_values(by=['counts'],ascending=True)
bins=[10,20,30,40,50,60,70]
plt.hist(train_cars_cnt_df['counts'].values, bins=bins, edgecolor="k")
plt.xticks(rotation=90)
plt.xlabel('Car counts bin')
plt.ylabel('Car count values')
plt.title("Bin distribution of Car images count",size=8)
plt.show()
## Showing some samples
train_cars.sample(15)
| car_name | image_size | car_image_class | |
|---|---|---|---|
| 5261 | Honda Odyssey Minivan 2012 | (392, 588, 3) | 126 |
| 7856 | Toyota Sequoia SUV 2012 | (450, 600, 3) | 186 |
| 5370 | Hyundai Accent Sedan 2012 | (348, 607, 3) | 136 |
| 848 | Audi S6 Sedan 2011 | (1200, 1600, 3) | 20 |
| 5632 | Hyundai Sonata Hybrid Sedan 2012 | (281, 600, 3) | 134 |
| 5859 | Infiniti QX56 SUV 2011 | (353, 607, 3) | 142 |
| 7859 | Toyota Sequoia SUV 2012 | (235, 500, 3) | 186 |
| 2398 | Chevrolet Corvette ZR1 2012 | (373, 592, 3) | 56 |
| 2132 | Cadillac SRX SUV 2012 | (480, 640, 3) | 52 |
| 4375 | Ford Edge SUV 2012 | (525, 700, 3) | 110 |
| 1193 | Bentley Continental GT Coupe 2012 | (300, 500, 3) | 42 |
| 7603 | Suzuki SX4 Hatchback 2012 | (1288, 1936, 3) | 183 |
| 941 | Audi TT RS Coupe 2012 | (360, 480, 3) | 25 |
| 6312 | Lamborghini Reventon Coupe 2008 | (414, 700, 3) | 150 |
| 1935 | Buick Rainier SUV 2007 | (210, 300, 3) | 48 |
del train_cars,train_cars_cnt, train_cars_cnt_df
## Reading annotations for train set and mapping to train images
test_images_upd= loadAnnot_map('Annotations/Test Annotation.csv',test_images)
## Sample
test_images_upd[0]
{'car_orig_image': array([[[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1],
...,
[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1]],
[[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1],
...,
[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1]],
[[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1],
...,
[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1]],
...,
[[16, 16, 16],
[16, 16, 16],
[16, 16, 16],
...,
[16, 16, 16],
[16, 16, 16],
[16, 16, 16]],
[[16, 16, 16],
[16, 16, 16],
[16, 16, 16],
...,
[18, 18, 18],
[17, 17, 17],
[17, 17, 17]],
[[16, 16, 16],
[16, 16, 16],
[16, 16, 16],
...,
[18, 18, 18],
[17, 17, 17],
[17, 17, 17]]], dtype=uint8),
'car_name': 'Audi R8 Coupe 2012',
'car_image_name': '08029.jpg',
'car_image_class': 15,
'bounding_box': (158, 200, 1457, 934)}
print("Number of images in updated test image list : {}".format(len(test_images_upd)))
Number of images in updated test image list : 8041
## Let us explore the test set
test_cars=pd.DataFrame([{'car_name': x['car_name'], 'image_size': x['car_orig_image'].shape, 'car_image_class':x['car_image_class']} for x in test_images_upd])
test_cars_cnt=test_cars['car_name'].value_counts(sort=True)
test_cars_cnt_df = pd.DataFrame(test_cars_cnt)
test_cars_cnt_df = test_cars_cnt_df.reset_index()
test_cars_cnt_df.columns = ['car_names', 'counts']
## Plotting by car images count - Top 10
from matplotlib import pyplot as plt
plt.figure(figsize=(8,5))
test_cars_cnt_df.sort_values(by=['counts'],ascending=False)
sns.barplot(data=test_cars_cnt_df.head(10), x='counts',y='car_names')
plt.xticks(rotation=90)
plt.title("Top 10 by Car images count",size=8)
plt.show()
## Plotting by car images count - Bottom 10
plt.figure(figsize=(8,5))
test_cars_cnt_df.sort_values(by=['counts'],ascending=False)
sns.barplot(data=test_cars_cnt_df.tail(10), x='counts',y='car_names')
plt.xticks(rotation=90)
plt.title("Bottom 10 by Car images count",size=8)
plt.show()
## Plotting car images count for each bin
plt.figure(figsize=(5,5))
test_cars_cnt_df.sort_values(by=['counts'],ascending=True)
bins=[10,20,30,40,50,60,70]
plt.hist(test_cars_cnt_df['counts'].values, bins=bins, edgecolor="k")
plt.xticks(rotation=90)
plt.xlabel('Car counts bin')
plt.ylabel('Car count values')
plt.title("Bin distribution of Car images count",size=8)
plt.show()
test_cars.sample(15)
| car_name | image_size | car_image_class | |
|---|---|---|---|
| 5905 | Jeep Compass SUV 2012 | (1067, 1600, 3) | 149 |
| 815 | Audi S6 Sedan 2011 | (250, 470, 3) | 20 |
| 4321 | Ford Edge SUV 2012 | (720, 960, 3) | 110 |
| 6765 | MINI Cooper Roadster Convertible 2012 | (512, 640, 3) | 157 |
| 7504 | Suzuki SX4 Hatchback 2012 | (492, 786, 3) | 183 |
| 3432 | Dodge Caravan Minivan 1997 | (375, 500, 3) | 85 |
| 3985 | Eagle Talon Hatchback 1998 | (300, 400, 3) | 98 |
| 5080 | Honda Accord Coupe 2012 | (768, 1024, 3) | 128 |
| 2340 | Chevrolet Corvette Ron Fellows Edition Z06 2007 | (69, 112, 3) | 57 |
| 2887 | Chevrolet Sonic Sedan 2012 | (450, 663, 3) | 63 |
| 3226 | Chrysler Sebring Convertible 2010 | (360, 424, 3) | 77 |
| 5302 | Hyundai Accent Sedan 2012 | (299, 450, 3) | 136 |
| 6434 | Mazda Tribute SUV 2011 | (1080, 1440, 3) | 159 |
| 3183 | Chrysler PT Cruiser Convertible 2008 | (427, 640, 3) | 81 |
| 698 | Audi S4 Sedan 2012 | (200, 300, 3) | 23 |
del test_images, test_cars, test_cars_cnt, test_cars_cnt_df
from matplotlib import pyplot as plt
def show_images(images_list,n):
rand = np.random.randint(0, len(images_list), n) # Generating n random numbers from total number of images
print("Showing images with index in : {}".format(rand))
plt.figure(figsize=(10, 20))
for cnt,j in enumerate(rand):
plt.subplot(n, 2, (cnt*2)+1)
plt.title(images_list[j]['car_name'])
plt.imshow(images_list[j]['car_orig_image'])
plt.subplot(n, 2, (cnt*2)+2)
temp_image=images_list[j]['car_orig_image'].copy()
gray = cv2.cvtColor(temp_image, cv2.COLOR_BGR2GRAY)
(x1, y1, x2, y2)=images_list[j]['bounding_box']
cv2.rectangle(temp_image, (x1,y1), (x2,y2), (255,0,0), 4)
plt.title(images_list[j]['car_name'])
plt.imshow(temp_image)
show_images(train_images_upd,5)
Showing images with index in : [5700 4181 7966 3086 3899]
show_images(test_images_upd,5)
Showing images with index in : [1132 3083 4255 5510 1329]
from matplotlib import pyplot as plt
def show_resized_images(images_list,n):
rand = np.random.randint(0, len(images_list), n) # Generating n random numbers from total number of images
print("Showing images with index in : {}".format(rand))
plt.figure(figsize=(10, 20))
for cnt,j in enumerate(rand):
plt.subplot(n, 2, (cnt*2)+1)
plt.title(images_list[j]['car_name'])
img_orig=images_list[j]['car_orig_image']
img_upd = cv2.resize(images_list[j]['car_orig_image'], dsize = (224,224))
plt.imshow(img_upd)
plt.subplot(n, 2, (cnt*2)+2)
temp_image=img_upd.copy()
(img_orig_h, img_orig_w, c)=img_orig.shape
gray = cv2.cvtColor(temp_image, cv2.COLOR_BGR2GRAY)
(x1, y1, x2, y2)=images_list[j]['bounding_box']
x1_=int((x1/img_orig_w)*224)
x2_=int((x2/img_orig_w)*224)
y1_=int((y1/img_orig_h)*224)
y2_=int((y2/img_orig_h)*224)
cv2.rectangle(temp_image, (x1_,y1_), (x2_,y2_), (255,0,0), 4)
plt.title(images_list[j]['car_name'])
plt.imshow(temp_image)
show_resized_images(train_images_upd,5)
Showing images with index in : [1882 2930 407 3542 5271]
show_resized_images(test_images_upd,5)
Showing images with index in : [7363 4925 3136 2300 1074]
Normalize the images
def normalize_images(images_list):
img_norm=[]
images_list_iter=iter(images_list)
for i in range(len(images_list)):
x=next(images_list_iter)
# Normalising pixel values
img=x['car_orig_image']
img_upd = (img / 255.).astype(np.float32)
#resizing to (128,128)
img_upd = cv2.resize(img_upd, dsize = (128,128))
img_norm.append(img_upd)
del img,img_upd
img_arr=np.array(img_norm)
return img_arr
X_train=normalize_images(train_images_upd)
X_train.shape
(8144, 128, 128, 3)
X_train[0]
array([[[0.40693146, 0.43825516, 0.42406845],
[0.56258523, 0.6316143 , 0.7239244 ],
[0.56564176, 0.6475964 , 0.7225045 ],
...,
[0.8117647 , 0.7921569 , 0.79607844],
[0.8189032 , 0.79929537, 0.80321693],
[0.8461091 , 0.8297182 , 0.8325674 ]],
[[0.44258076, 0.4790068 , 0.47279078],
[0.5042842 , 0.5789216 , 0.6645427 ],
[0.5265848 , 0.60241747, 0.67387193],
...,
[0.8117647 , 0.7921569 , 0.79607844],
[0.8189032 , 0.79929537, 0.80321693],
[0.8461091 , 0.8297182 , 0.8325674 ]],
[[0.5176861 , 0.5562891 , 0.57163835],
[0.5475268 , 0.62560725, 0.70842814],
[0.5407605 , 0.6103925 , 0.6789716 ],
...,
[0.8117647 , 0.7921569 , 0.79607844],
[0.8189032 , 0.79929537, 0.80321693],
[0.8461091 , 0.8297182 , 0.8325674 ]],
...,
[[0.52763486, 0.53939956, 0.55508584],
[0.53011644, 0.54188114, 0.5575674 ],
[0.5312129 , 0.54297763, 0.5586639 ],
...,
[0.80440843, 0.80440843, 0.80440843],
[0.86936295, 0.86936295, 0.86936295],
[0.64076334, 0.64076334, 0.64076334]],
[[0.54911155, 0.56087625, 0.5765625 ],
[0.5503933 , 0.562158 , 0.57784426],
[0.53894 , 0.5507047 , 0.566391 ],
...,
[0.55986357, 0.55986357, 0.55986357],
[0.66391844, 0.66391844, 0.66391844],
[0.631841 , 0.631841 , 0.631841 ]],
[[0.54792744, 0.55969214, 0.5753784 ],
[0.5502145 , 0.56197923, 0.5776655 ],
[0.5431373 , 0.554902 , 0.5705883 ],
...,
[0.5628734 , 0.5628734 , 0.5628734 ],
[0.54403967, 0.54403967, 0.54403967],
[0.6291253 , 0.6291253 , 0.6291253 ]]], dtype=float32)
y_train=pd.DataFrame([x['car_image_class'] for x in train_images_upd])
y_train.shape
(8144, 1)
y_train
| 0 | |
|---|---|
| 0 | 15 |
| 1 | 15 |
| 2 | 15 |
| 3 | 15 |
| 4 | 15 |
| ... | ... |
| 8139 | 177 |
| 8140 | 177 |
| 8141 | 177 |
| 8142 | 177 |
| 8143 | 177 |
8144 rows × 1 columns
del train_images_upd
del test_images
X_test=normalize_images(test_images_upd)
X_test.shape
(8041, 128, 128, 3)
X_test[0]
array([[[0.00392157, 0.00392157, 0.00392157],
[0.00392157, 0.00392157, 0.00392157],
[0.00392157, 0.00392157, 0.00392157],
...,
[0.01488971, 0.01488971, 0.01488971],
[0.01624876, 0.01624876, 0.01624876],
[0.00925245, 0.00925245, 0.00925245]],
[[0.00392157, 0.00392157, 0.00392157],
[0.00392157, 0.00392157, 0.00392157],
[0.00392157, 0.00392157, 0.00392157],
...,
[0.00943005, 0.00943005, 0.00943005],
[0.01568628, 0.01568628, 0.01568628],
[0.01176471, 0.01176471, 0.01176471]],
[[0.00392157, 0.00392157, 0.00392157],
[0.00392157, 0.00392157, 0.00392157],
[0.00392157, 0.00392157, 0.00392157],
...,
[0.01176471, 0.01176471, 0.01176471],
[0.02291667, 0.02291667, 0.02291667],
[0.01568628, 0.01568628, 0.01568628]],
...,
[[0.07129289, 0.07129289, 0.07129289],
[0.08125 , 0.08125 , 0.08125 ],
[0.12062892, 0.12062892, 0.12062892],
...,
[0.0541973 , 0.0541973 , 0.0541973 ],
[0.06898265, 0.06898265, 0.06898265],
[0.06063113, 0.06063113, 0.06063113]],
[[0.07450981, 0.07450981, 0.07450981],
[0.08627451, 0.08627451, 0.08627451],
[0.10821079, 0.10821079, 0.10821079],
...,
[0.0627451 , 0.0627451 , 0.0627451 ],
[0.05882353, 0.05882353, 0.05882353],
[0.05490196, 0.05490196, 0.05490196]],
[[0.07058824, 0.07058824, 0.07058824],
[0.08632477, 0.08632477, 0.08632477],
[0.09396446, 0.09396446, 0.09396446],
...,
[0.04364995, 0.04364995, 0.04364995],
[0.05882353, 0.05882353, 0.05882353],
[0.05882353, 0.05882353, 0.05882353]]], dtype=float32)
y_test=pd.DataFrame([x['car_image_class'] for x in test_images_upd])
y_test.shape
(8041, 1)
y_test
| 0 | |
|---|---|
| 0 | 15 |
| 1 | 15 |
| 2 | 15 |
| 3 | 15 |
| 4 | 15 |
| ... | ... |
| 8036 | 177 |
| 8037 | 177 |
| 8038 | 177 |
| 8039 | 177 |
| 8040 | 177 |
8041 rows × 1 columns
del test_images_upd
print("Shape of X_train : {}".format(X_train.shape))
print("Shape of y_train : {}".format(y_train.shape))
print("Shape of X_test : {}".format(X_test.shape))
print("Shape of y_test : {}".format(y_test.shape))
Shape of X_train : (8144, 128, 128, 3) Shape of y_train : (8144, 1) Shape of X_test : (8041, 128, 128, 3) Shape of y_test : (8041, 1)
# encode labels for y_train
from keras.utils import np_utils
le = LabelEncoder()
le.fit(y_train[0])
encoded_labels_train = le.transform(y_train[0])
y_train_encoded = np_utils.to_categorical(encoded_labels_train)
encoded_labels_test = le.transform(y_test[0])
y_test_encoded = np_utils.to_categorical(encoded_labels_test)
class_dict={l: i for (i, l) in enumerate(le.classes_)}
y_train_encoded
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)
y_test_encoded
array([[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.],
[0., 0., 0., ..., 0., 0., 0.]], dtype=float32)
Basic CNN classifier model
# Initialising the CNN classifier
cnn_classifier = Sequential()
# Add a Convolution layer with 32 kernels of 3X3 shape with activation function ReLU
cnn_classifier.add(Conv2D(32, (3, 3), input_shape = (128,128,3), activation = 'relu', padding = 'same'))
# Add a Max Pooling layer of size 2X2
cnn_classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Add another Convolution layer with 32 kernels of 3X3 shape with activation function ReLU
cnn_classifier.add(Conv2D(64, (3, 3), activation = 'relu', padding = 'same'))
# Adding another pooling layer
cnn_classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Adding dropout with probability 0.4
cnn_classifier.add(Dropout(0.4))
# Flattening the layer before fully connected layers
cnn_classifier.add(Flatten())
# Adding a fully connected layer with 256 neurons
cnn_classifier.add(Dense(units = 512, activation = 'relu'))
# Adding dropout with probability 0.4
cnn_classifier.add(Dropout(0.4))
# Adding a fully connected layer with 256 neurons
cnn_classifier.add(Dense(units = 256, activation = 'relu'))
# The final output layer with 17 neuron to predict the categorical classifcation
cnn_classifier.add(Dense(units = 196, activation = 'softmax'))
opt = Adam()
cnn_classifier.compile(optimizer = opt, loss = 'categorical_crossentropy', metrics = ['accuracy'])
base_cnn_model = cnn_classifier.fit(X_train, y_train_encoded, validation_data=(X_test, y_test_encoded), epochs=30, batch_size=64, verbose=1)
Epoch 1/30 128/128 [==============================] - 12s 68ms/step - loss: 5.3157 - accuracy: 0.0049 - val_loss: 5.2751 - val_accuracy: 0.0103 Epoch 2/30 128/128 [==============================] - 7s 53ms/step - loss: 5.2203 - accuracy: 0.0112 - val_loss: 5.1581 - val_accuracy: 0.0147 Epoch 3/30 128/128 [==============================] - 7s 54ms/step - loss: 5.0838 - accuracy: 0.0193 - val_loss: 5.0591 - val_accuracy: 0.0160 Epoch 4/30 128/128 [==============================] - 7s 53ms/step - loss: 4.6949 - accuracy: 0.0580 - val_loss: 5.0258 - val_accuracy: 0.0287 Epoch 5/30 128/128 [==============================] - 7s 53ms/step - loss: 3.7525 - accuracy: 0.1902 - val_loss: 5.2733 - val_accuracy: 0.0323 Epoch 6/30 128/128 [==============================] - 7s 53ms/step - loss: 2.4539 - accuracy: 0.4357 - val_loss: 5.9870 - val_accuracy: 0.0317 Epoch 7/30 128/128 [==============================] - 7s 53ms/step - loss: 1.3999 - accuracy: 0.6600 - val_loss: 6.6927 - val_accuracy: 0.0325 Epoch 8/30 128/128 [==============================] - 7s 53ms/step - loss: 0.8342 - accuracy: 0.7947 - val_loss: 7.3083 - val_accuracy: 0.0320 Epoch 9/30 128/128 [==============================] - 7s 53ms/step - loss: 0.5365 - accuracy: 0.8685 - val_loss: 7.5566 - val_accuracy: 0.0354 Epoch 10/30 128/128 [==============================] - 7s 53ms/step - loss: 0.4251 - accuracy: 0.8911 - val_loss: 7.9295 - val_accuracy: 0.0311 Epoch 11/30 128/128 [==============================] - 7s 53ms/step - loss: 0.3316 - accuracy: 0.9159 - val_loss: 8.1083 - val_accuracy: 0.0323 Epoch 12/30 128/128 [==============================] - 7s 53ms/step - loss: 0.2970 - accuracy: 0.9310 - val_loss: 7.7673 - val_accuracy: 0.0338 Epoch 13/30 128/128 [==============================] - 7s 53ms/step - loss: 0.2299 - accuracy: 0.9435 - val_loss: 8.0458 - val_accuracy: 0.0336 Epoch 14/30 128/128 [==============================] - 7s 53ms/step - loss: 0.2339 - accuracy: 0.9431 - val_loss: 8.1504 - val_accuracy: 0.0330 Epoch 15/30 128/128 [==============================] - 7s 53ms/step - loss: 0.1955 - accuracy: 0.9544 - val_loss: 8.3594 - val_accuracy: 0.0358 Epoch 16/30 128/128 [==============================] - 7s 54ms/step - loss: 0.1680 - accuracy: 0.9591 - val_loss: 8.3860 - val_accuracy: 0.0335 Epoch 17/30 128/128 [==============================] - 7s 54ms/step - loss: 0.1656 - accuracy: 0.9583 - val_loss: 8.5168 - val_accuracy: 0.0347 Epoch 18/30 128/128 [==============================] - 7s 54ms/step - loss: 0.1500 - accuracy: 0.9621 - val_loss: 8.4768 - val_accuracy: 0.0348 Epoch 19/30 128/128 [==============================] - 7s 53ms/step - loss: 0.1482 - accuracy: 0.9648 - val_loss: 8.4776 - val_accuracy: 0.0363 Epoch 20/30 128/128 [==============================] - 7s 53ms/step - loss: 0.1491 - accuracy: 0.9630 - val_loss: 8.9374 - val_accuracy: 0.0327 Epoch 21/30 128/128 [==============================] - 7s 53ms/step - loss: 0.1391 - accuracy: 0.9668 - val_loss: 8.6691 - val_accuracy: 0.0325 Epoch 22/30 128/128 [==============================] - 7s 53ms/step - loss: 0.1186 - accuracy: 0.9692 - val_loss: 8.9904 - val_accuracy: 0.0353 Epoch 23/30 128/128 [==============================] - 7s 53ms/step - loss: 0.1267 - accuracy: 0.9710 - val_loss: 8.8426 - val_accuracy: 0.0349 Epoch 24/30 128/128 [==============================] - 7s 53ms/step - loss: 0.1076 - accuracy: 0.9715 - val_loss: 8.7803 - val_accuracy: 0.0320 Epoch 25/30 128/128 [==============================] - 7s 54ms/step - loss: 0.1139 - accuracy: 0.9713 - val_loss: 9.1675 - val_accuracy: 0.0322 Epoch 26/30 128/128 [==============================] - 7s 54ms/step - loss: 0.1277 - accuracy: 0.9684 - val_loss: 9.0108 - val_accuracy: 0.0315 Epoch 27/30 128/128 [==============================] - 7s 53ms/step - loss: 0.1043 - accuracy: 0.9735 - val_loss: 9.2415 - val_accuracy: 0.0343 Epoch 28/30 128/128 [==============================] - 7s 53ms/step - loss: 0.1083 - accuracy: 0.9734 - val_loss: 8.6566 - val_accuracy: 0.0325 Epoch 29/30 128/128 [==============================] - 7s 58ms/step - loss: 0.1089 - accuracy: 0.9737 - val_loss: 8.7271 - val_accuracy: 0.0312 Epoch 30/30 128/128 [==============================] - 7s 53ms/step - loss: 0.1018 - accuracy: 0.9736 - val_loss: 8.8515 - val_accuracy: 0.0332
base_model_result = cnn_classifier.evaluate(X_test, y_test_encoded)
252/252 [==============================] - 2s 9ms/step - loss: 8.8515 - accuracy: 0.0332
print('Test loss: ', base_model_result[0])
print('Test accuracy: ', base_model_result[1])
Test loss: 8.851463317871094 Test accuracy: 0.0332048237323761
The model has produced 97.36% accuracy in train set and just 3.32% accuracy in test set.
The model has overfitted in training set, giving very low accuracy in test set.This model needs further hyperparameter tuning. We may need to apply transfer learning/combine few more models in order to improve accuracy.
## Plotting Train and Test loss
epoch=30
loss_train = base_cnn_model.history['loss']
loss_val = base_cnn_model.history['val_loss']
epochs = range(1,epoch+1)
plt.plot(epochs, loss_train, 'g', label='Training loss')
plt.plot(epochs, loss_val, 'b', label='validation loss')
plt.title('Training and Validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
## Plotting Train and Test accuracy
Acc_train = base_cnn_model.history['accuracy']
Acc_val = base_cnn_model.history['val_accuracy']
epochs = range(1,epoch+1)
plt.plot(epochs, Acc_train, 'g', label='Training accuracy')
plt.plot(epochs, Acc_val, 'b', label='validation accuracy')
plt.title('Training and Validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('accuracy')
plt.legend()
plt.show()
# Saving the model for future use
cnn_classifier.save('./base_model.h5')
cnn_classifier.save_weights('./base_model_weights.h5')
Tuning CNN model with Image Augmentation
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
valid_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
batch_size = 32
train_generator = train_datagen.flow(x=X_train,y=y_train_encoded,
batch_size=batch_size,
seed=52,
shuffle=True)
valid_generator = valid_datagen.flow(x=X_test,y=y_test_encoded,
batch_size=batch_size,
seed=52)
cnn_classifier.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
cnn_history=cnn_classifier.fit(train_generator,
validation_data = valid_generator,
epochs=30,
verbose=1)
Epoch 1/30 255/255 [==============================] - 8s 27ms/step - loss: 289.3972 - accuracy: 0.0050 - val_loss: 5.2843 - val_accuracy: 0.0052 Epoch 2/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2911 - accuracy: 0.0044 - val_loss: 5.2793 - val_accuracy: 0.0052 Epoch 3/30 255/255 [==============================] - 7s 28ms/step - loss: 5.2866 - accuracy: 0.0052 - val_loss: 5.2783 - val_accuracy: 0.0052 Epoch 4/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2845 - accuracy: 0.0041 - val_loss: 5.2780 - val_accuracy: 0.0052 Epoch 5/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2806 - accuracy: 0.0045 - val_loss: 5.2777 - val_accuracy: 0.0052 Epoch 6/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2798 - accuracy: 0.0045 - val_loss: 5.2774 - val_accuracy: 0.0052 Epoch 7/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2795 - accuracy: 0.0048 - val_loss: 5.2772 - val_accuracy: 0.0052 Epoch 8/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2796 - accuracy: 0.0048 - val_loss: 5.2770 - val_accuracy: 0.0052 Epoch 9/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2782 - accuracy: 0.0053 - val_loss: 5.2769 - val_accuracy: 0.0052 Epoch 10/30 255/255 [==============================] - 7s 26ms/step - loss: 5.2799 - accuracy: 0.0053 - val_loss: 5.2767 - val_accuracy: 0.0052 Epoch 11/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2787 - accuracy: 0.0058 - val_loss: 5.2766 - val_accuracy: 0.0052 Epoch 12/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2785 - accuracy: 0.0065 - val_loss: 5.2765 - val_accuracy: 0.0085 Epoch 13/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2777 - accuracy: 0.0064 - val_loss: 5.2764 - val_accuracy: 0.0085 Epoch 14/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2779 - accuracy: 0.0065 - val_loss: 5.2764 - val_accuracy: 0.0085 Epoch 15/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2773 - accuracy: 0.0075 - val_loss: 5.2763 - val_accuracy: 0.0085 Epoch 16/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2773 - accuracy: 0.0068 - val_loss: 5.2761 - val_accuracy: 0.0085 Epoch 17/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2768 - accuracy: 0.0079 - val_loss: 5.2760 - val_accuracy: 0.0085 Epoch 18/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2770 - accuracy: 0.0079 - val_loss: 5.2760 - val_accuracy: 0.0085 Epoch 19/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2761 - accuracy: 0.0085 - val_loss: 5.2759 - val_accuracy: 0.0085 Epoch 20/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2765 - accuracy: 0.0080 - val_loss: 5.2758 - val_accuracy: 0.0085 Epoch 21/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2761 - accuracy: 0.0087 - val_loss: 5.2757 - val_accuracy: 0.0085 Epoch 22/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2768 - accuracy: 0.0086 - val_loss: 5.2756 - val_accuracy: 0.0085 Epoch 23/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2761 - accuracy: 0.0081 - val_loss: 5.2755 - val_accuracy: 0.0085 Epoch 24/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2763 - accuracy: 0.0082 - val_loss: 5.2754 - val_accuracy: 0.0085 Epoch 25/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2767 - accuracy: 0.0085 - val_loss: 5.2754 - val_accuracy: 0.0085 Epoch 26/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2762 - accuracy: 0.0086 - val_loss: 5.2753 - val_accuracy: 0.0085 Epoch 27/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2761 - accuracy: 0.0083 - val_loss: 5.2752 - val_accuracy: 0.0085 Epoch 28/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2760 - accuracy: 0.0083 - val_loss: 5.2751 - val_accuracy: 0.0085 Epoch 29/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2759 - accuracy: 0.0083 - val_loss: 5.2751 - val_accuracy: 0.0085 Epoch 30/30 255/255 [==============================] - 7s 27ms/step - loss: 5.2758 - accuracy: 0.0083 - val_loss: 5.2750 - val_accuracy: 0.0085
## Plotting Train and Test loss
epoch=30
loss_train = cnn_history.history['loss']
loss_val = cnn_history.history['val_loss']
epochs = range(1,epoch+1)
plt.plot(epochs, loss_train, 'g', label='Training loss')
plt.plot(epochs, loss_val, 'b', label='validation loss')
plt.title('Training and Validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
## Plotting Train and Test accuracy
Acc_train = cnn_history.history['accuracy']
Acc_val = cnn_history.history['val_accuracy']
epochs = range(1,epoch+1)
plt.plot(epochs, Acc_train, 'g', label='Training accuracy')
plt.plot(epochs, Acc_val, 'b', label='validation accuracy')
plt.title('Training and Validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('accuracy')
plt.legend()
plt.show()
From the above graphs, it is evident that CNN model with image augmentation did not give good predictions either. Even the predictions in train set is not good.
Let us try transfer learning using few famous architectures
VGG19 model for classifying the car models
from tensorflow.keras.applications import VGG19
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
checkpoint = ModelCheckpoint("model-{loss:.2f}.h5", monitor="loss", verbose=1, save_best_only=True,
save_weights_only=True, mode="min")
stop = EarlyStopping(monitor="loss", patience=5, mode="min")
reduce_lr = ReduceLROnPlateau(monitor="loss", factor=0.2, patience=5, min_lr=1e-6, verbose=1, mode="min")
vgg_base_model = VGG19(input_shape = (128, 128, 3),
weights='imagenet',
include_top=False)
x = vgg_base_model.output
x = GlobalAveragePooling2D()(x) # Optional
x = Dense(1024, activation='relu')(x) # dense layer 1
x = Dropout(0.2)(x) # dropout
x = Dense(512, activation='relu')(x) # dense layer 2
x = Dropout(0.2)(x) # dropout
x = Dense(256, activation='relu')(x) # dense layer 3
preds = Dense(196, activation='softmax')(x) # final layer with softmax activation
model_vgg19 = Model(inputs=vgg_base_model.input,
outputs=preds)
# set the first 20 layers of the network to be non-trainable
for layer in model_vgg19.layers[:20]:
layer.trainable=False
for layer in model_vgg19.layers[20:]:
layer.trainable=True
# Compile the model
opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.001, amsgrad=False)
model_vgg19.compile(optimizer=opt,
loss='categorical_crossentropy',
metrics=['categorical_accuracy'])
model_vgg19.summary()
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 128, 128, 3)] 0
block1_conv1 (Conv2D) (None, 128, 128, 64) 1792
block1_conv2 (Conv2D) (None, 128, 128, 64) 36928
block1_pool (MaxPooling2D) (None, 64, 64, 64) 0
block2_conv1 (Conv2D) (None, 64, 64, 128) 73856
block2_conv2 (Conv2D) (None, 64, 64, 128) 147584
block2_pool (MaxPooling2D) (None, 32, 32, 128) 0
block3_conv1 (Conv2D) (None, 32, 32, 256) 295168
block3_conv2 (Conv2D) (None, 32, 32, 256) 590080
block3_conv3 (Conv2D) (None, 32, 32, 256) 590080
block3_conv4 (Conv2D) (None, 32, 32, 256) 590080
block3_pool (MaxPooling2D) (None, 16, 16, 256) 0
block4_conv1 (Conv2D) (None, 16, 16, 512) 1180160
block4_conv2 (Conv2D) (None, 16, 16, 512) 2359808
block4_conv3 (Conv2D) (None, 16, 16, 512) 2359808
block4_conv4 (Conv2D) (None, 16, 16, 512) 2359808
block4_pool (MaxPooling2D) (None, 8, 8, 512) 0
block5_conv1 (Conv2D) (None, 8, 8, 512) 2359808
block5_conv2 (Conv2D) (None, 8, 8, 512) 2359808
block5_conv3 (Conv2D) (None, 8, 8, 512) 2359808
block5_conv4 (Conv2D) (None, 8, 8, 512) 2359808
block5_pool (MaxPooling2D) (None, 4, 4, 512) 0
global_average_pooling2d (G (None, 512) 0
lobalAveragePooling2D)
dense_3 (Dense) (None, 1024) 525312
dropout_2 (Dropout) (None, 1024) 0
dense_4 (Dense) (None, 512) 524800
dropout_3 (Dropout) (None, 512) 0
dense_5 (Dense) (None, 256) 131328
dense_6 (Dense) (None, 196) 50372
=================================================================
Total params: 21,256,196
Trainable params: 3,591,620
Non-trainable params: 17,664,576
_________________________________________________________________
/usr/local/lib/python3.8/dist-packages/keras/optimizers/optimizer_v2/adam.py:110: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead. super(Adam, self).__init__(name, **kwargs)
vgg19_model = model_vgg19.fit(X_train, y_train_encoded, validation_data=(X_test, y_test_encoded),callbacks=[reduce_lr, stop], epochs=100, verbose=1)
Epoch 1/100 255/255 [==============================] - 37s 136ms/step - loss: 5.2812 - categorical_accuracy: 0.0049 - val_loss: 5.2334 - val_categorical_accuracy: 0.0146 - lr: 0.0010 Epoch 2/100 255/255 [==============================] - 32s 126ms/step - loss: 4.9939 - categorical_accuracy: 0.0205 - val_loss: 4.6969 - val_categorical_accuracy: 0.0377 - lr: 0.0010 Epoch 3/100 255/255 [==============================] - 32s 124ms/step - loss: 4.4713 - categorical_accuracy: 0.0456 - val_loss: 4.3350 - val_categorical_accuracy: 0.0603 - lr: 0.0010 Epoch 4/100 255/255 [==============================] - 32s 125ms/step - loss: 4.0990 - categorical_accuracy: 0.0815 - val_loss: 4.1141 - val_categorical_accuracy: 0.0842 - lr: 0.0010 Epoch 5/100 255/255 [==============================] - 32s 126ms/step - loss: 3.7650 - categorical_accuracy: 0.1138 - val_loss: 3.9846 - val_categorical_accuracy: 0.0995 - lr: 0.0010 Epoch 6/100 255/255 [==============================] - 32s 125ms/step - loss: 3.4509 - categorical_accuracy: 0.1566 - val_loss: 3.9260 - val_categorical_accuracy: 0.1094 - lr: 0.0010 Epoch 7/100 255/255 [==============================] - 32s 125ms/step - loss: 3.1345 - categorical_accuracy: 0.2021 - val_loss: 3.8805 - val_categorical_accuracy: 0.1229 - lr: 0.0010 Epoch 8/100 255/255 [==============================] - 32s 125ms/step - loss: 2.8429 - categorical_accuracy: 0.2436 - val_loss: 3.8735 - val_categorical_accuracy: 0.1318 - lr: 0.0010 Epoch 9/100 255/255 [==============================] - 32s 125ms/step - loss: 2.5713 - categorical_accuracy: 0.2933 - val_loss: 3.9836 - val_categorical_accuracy: 0.1398 - lr: 0.0010 Epoch 10/100 255/255 [==============================] - 32s 125ms/step - loss: 2.3128 - categorical_accuracy: 0.3491 - val_loss: 4.0177 - val_categorical_accuracy: 0.1517 - lr: 0.0010 Epoch 11/100 255/255 [==============================] - 32s 125ms/step - loss: 2.0588 - categorical_accuracy: 0.4101 - val_loss: 4.2203 - val_categorical_accuracy: 0.1517 - lr: 0.0010 Epoch 12/100 255/255 [==============================] - 32s 125ms/step - loss: 1.8181 - categorical_accuracy: 0.4660 - val_loss: 4.4829 - val_categorical_accuracy: 0.1593 - lr: 0.0010 Epoch 13/100 255/255 [==============================] - 32s 125ms/step - loss: 1.6146 - categorical_accuracy: 0.5141 - val_loss: 4.6753 - val_categorical_accuracy: 0.1538 - lr: 0.0010 Epoch 14/100 255/255 [==============================] - 32s 125ms/step - loss: 1.4287 - categorical_accuracy: 0.5651 - val_loss: 4.8330 - val_categorical_accuracy: 0.1624 - lr: 0.0010 Epoch 15/100 255/255 [==============================] - 32s 125ms/step - loss: 1.2570 - categorical_accuracy: 0.6088 - val_loss: 5.1718 - val_categorical_accuracy: 0.1629 - lr: 0.0010 Epoch 16/100 255/255 [==============================] - 32s 125ms/step - loss: 1.0930 - categorical_accuracy: 0.6620 - val_loss: 5.4079 - val_categorical_accuracy: 0.1627 - lr: 0.0010 Epoch 17/100 255/255 [==============================] - 32s 125ms/step - loss: 0.9768 - categorical_accuracy: 0.6994 - val_loss: 5.6456 - val_categorical_accuracy: 0.1632 - lr: 0.0010 Epoch 18/100 255/255 [==============================] - 32s 125ms/step - loss: 0.8351 - categorical_accuracy: 0.7369 - val_loss: 6.0675 - val_categorical_accuracy: 0.1640 - lr: 0.0010 Epoch 19/100 255/255 [==============================] - 32s 125ms/step - loss: 0.7528 - categorical_accuracy: 0.7668 - val_loss: 6.2897 - val_categorical_accuracy: 0.1613 - lr: 0.0010 Epoch 20/100 255/255 [==============================] - 32s 125ms/step - loss: 0.6578 - categorical_accuracy: 0.7995 - val_loss: 6.5047 - val_categorical_accuracy: 0.1643 - lr: 0.0010 Epoch 21/100 255/255 [==============================] - 32s 125ms/step - loss: 0.5832 - categorical_accuracy: 0.8206 - val_loss: 6.7648 - val_categorical_accuracy: 0.1623 - lr: 0.0010 Epoch 22/100 255/255 [==============================] - 32s 125ms/step - loss: 0.5241 - categorical_accuracy: 0.8403 - val_loss: 7.0967 - val_categorical_accuracy: 0.1620 - lr: 0.0010 Epoch 23/100 255/255 [==============================] - 32s 125ms/step - loss: 0.4520 - categorical_accuracy: 0.8619 - val_loss: 7.2380 - val_categorical_accuracy: 0.1589 - lr: 0.0010 Epoch 24/100 255/255 [==============================] - 32s 125ms/step - loss: 0.3955 - categorical_accuracy: 0.8805 - val_loss: 7.6025 - val_categorical_accuracy: 0.1578 - lr: 0.0010 Epoch 25/100 255/255 [==============================] - 32s 125ms/step - loss: 0.3736 - categorical_accuracy: 0.8886 - val_loss: 7.9167 - val_categorical_accuracy: 0.1576 - lr: 0.0010 Epoch 26/100 255/255 [==============================] - 32s 125ms/step - loss: 0.3330 - categorical_accuracy: 0.9028 - val_loss: 8.1276 - val_categorical_accuracy: 0.1581 - lr: 0.0010 Epoch 27/100 255/255 [==============================] - 32s 125ms/step - loss: 0.2959 - categorical_accuracy: 0.9129 - val_loss: 8.3936 - val_categorical_accuracy: 0.1540 - lr: 0.0010 Epoch 28/100 255/255 [==============================] - 32s 125ms/step - loss: 0.2705 - categorical_accuracy: 0.9235 - val_loss: 8.6862 - val_categorical_accuracy: 0.1572 - lr: 0.0010 Epoch 29/100 255/255 [==============================] - 32s 126ms/step - loss: 0.2421 - categorical_accuracy: 0.9322 - val_loss: 8.8822 - val_categorical_accuracy: 0.1574 - lr: 0.0010 Epoch 30/100 255/255 [==============================] - 32s 125ms/step - loss: 0.2156 - categorical_accuracy: 0.9407 - val_loss: 9.1672 - val_categorical_accuracy: 0.1528 - lr: 0.0010 Epoch 31/100 255/255 [==============================] - 32s 125ms/step - loss: 0.1995 - categorical_accuracy: 0.9460 - val_loss: 9.2521 - val_categorical_accuracy: 0.1588 - lr: 0.0010 Epoch 32/100 255/255 [==============================] - 32s 125ms/step - loss: 0.1834 - categorical_accuracy: 0.9487 - val_loss: 9.5684 - val_categorical_accuracy: 0.1469 - lr: 0.0010 Epoch 33/100 255/255 [==============================] - 32s 125ms/step - loss: 0.1711 - categorical_accuracy: 0.9549 - val_loss: 9.4449 - val_categorical_accuracy: 0.1583 - lr: 0.0010 Epoch 34/100 255/255 [==============================] - 32s 125ms/step - loss: 0.1569 - categorical_accuracy: 0.9586 - val_loss: 9.8361 - val_categorical_accuracy: 0.1589 - lr: 0.0010 Epoch 35/100 255/255 [==============================] - 32s 125ms/step - loss: 0.1469 - categorical_accuracy: 0.9623 - val_loss: 9.9113 - val_categorical_accuracy: 0.1557 - lr: 0.0010 Epoch 36/100 255/255 [==============================] - 32s 125ms/step - loss: 0.1431 - categorical_accuracy: 0.9633 - val_loss: 10.0079 - val_categorical_accuracy: 0.1533 - lr: 0.0010 Epoch 37/100 255/255 [==============================] - 32s 125ms/step - loss: 0.1319 - categorical_accuracy: 0.9672 - val_loss: 10.2578 - val_categorical_accuracy: 0.1521 - lr: 0.0010 Epoch 38/100 255/255 [==============================] - 32s 125ms/step - loss: 0.1150 - categorical_accuracy: 0.9714 - val_loss: 10.3879 - val_categorical_accuracy: 0.1582 - lr: 0.0010 Epoch 39/100 255/255 [==============================] - 32s 125ms/step - loss: 0.1113 - categorical_accuracy: 0.9729 - val_loss: 10.4416 - val_categorical_accuracy: 0.1568 - lr: 0.0010 Epoch 40/100 255/255 [==============================] - 32s 125ms/step - loss: 0.1047 - categorical_accuracy: 0.9746 - val_loss: 10.4957 - val_categorical_accuracy: 0.1563 - lr: 0.0010 Epoch 41/100 255/255 [==============================] - 32s 125ms/step - loss: 0.1016 - categorical_accuracy: 0.9736 - val_loss: 10.7816 - val_categorical_accuracy: 0.1492 - lr: 0.0010 Epoch 42/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0946 - categorical_accuracy: 0.9758 - val_loss: 10.8335 - val_categorical_accuracy: 0.1535 - lr: 0.0010 Epoch 43/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0896 - categorical_accuracy: 0.9785 - val_loss: 10.9289 - val_categorical_accuracy: 0.1521 - lr: 0.0010 Epoch 44/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0828 - categorical_accuracy: 0.9808 - val_loss: 11.2373 - val_categorical_accuracy: 0.1523 - lr: 0.0010 Epoch 45/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0739 - categorical_accuracy: 0.9843 - val_loss: 11.2400 - val_categorical_accuracy: 0.1530 - lr: 0.0010 Epoch 46/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0762 - categorical_accuracy: 0.9823 - val_loss: 11.3315 - val_categorical_accuracy: 0.1551 - lr: 0.0010 Epoch 47/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0756 - categorical_accuracy: 0.9822 - val_loss: 11.4029 - val_categorical_accuracy: 0.1522 - lr: 0.0010 Epoch 48/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0684 - categorical_accuracy: 0.9839 - val_loss: 11.4388 - val_categorical_accuracy: 0.1501 - lr: 0.0010 Epoch 49/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0669 - categorical_accuracy: 0.9827 - val_loss: 11.4943 - val_categorical_accuracy: 0.1543 - lr: 0.0010 Epoch 50/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0618 - categorical_accuracy: 0.9850 - val_loss: 11.8429 - val_categorical_accuracy: 0.1556 - lr: 0.0010 Epoch 51/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0610 - categorical_accuracy: 0.9843 - val_loss: 11.8097 - val_categorical_accuracy: 0.1547 - lr: 0.0010 Epoch 52/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0565 - categorical_accuracy: 0.9865 - val_loss: 11.8550 - val_categorical_accuracy: 0.1548 - lr: 0.0010 Epoch 53/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0595 - categorical_accuracy: 0.9856 - val_loss: 12.0019 - val_categorical_accuracy: 0.1515 - lr: 0.0010 Epoch 54/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0525 - categorical_accuracy: 0.9887 - val_loss: 12.1067 - val_categorical_accuracy: 0.1527 - lr: 0.0010 Epoch 55/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0473 - categorical_accuracy: 0.9903 - val_loss: 12.1429 - val_categorical_accuracy: 0.1506 - lr: 0.0010 Epoch 56/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0500 - categorical_accuracy: 0.9881 - val_loss: 12.2153 - val_categorical_accuracy: 0.1491 - lr: 0.0010 Epoch 57/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0508 - categorical_accuracy: 0.9878 - val_loss: 12.1417 - val_categorical_accuracy: 0.1555 - lr: 0.0010 Epoch 58/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0443 - categorical_accuracy: 0.9898 - val_loss: 12.4283 - val_categorical_accuracy: 0.1558 - lr: 0.0010 Epoch 59/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0466 - categorical_accuracy: 0.9881 - val_loss: 12.2771 - val_categorical_accuracy: 0.1606 - lr: 0.0010 Epoch 60/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0478 - categorical_accuracy: 0.9874 - val_loss: 12.5476 - val_categorical_accuracy: 0.1495 - lr: 0.0010 Epoch 61/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0481 - categorical_accuracy: 0.9871 - val_loss: 12.5606 - val_categorical_accuracy: 0.1527 - lr: 0.0010 Epoch 62/100 255/255 [==============================] - 32s 125ms/step - loss: 0.0489 - categorical_accuracy: 0.9871 - val_loss: 12.4304 - val_categorical_accuracy: 0.1566 - lr: 0.0010 Epoch 63/100 254/255 [============================>.] - ETA: 0s - loss: 0.0464 - categorical_accuracy: 0.9887 Epoch 63: ReduceLROnPlateau reducing learning rate to 0.00020000000949949026. 255/255 [==============================] - 32s 125ms/step - loss: 0.0464 - categorical_accuracy: 0.9887 - val_loss: 12.6165 - val_categorical_accuracy: 0.1541 - lr: 0.0010
## Plotting Train and Test loss
epoch=63
loss_train = vgg19_model.history['loss']
loss_val = vgg19_model.history['val_loss']
epochs = range(1,epoch+1)
plt.plot(epochs, loss_train, 'g', label='Training loss')
plt.plot(epochs, loss_val, 'b', label='validation loss')
plt.title('Training and Validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
## Plotting Train and Test accuracy
Acc_train = vgg19_model.history['categorical_accuracy']
Acc_val = vgg19_model.history['val_categorical_accuracy']
epochs = range(1,epoch+1)
plt.plot(epochs, Acc_train, 'g', label='Training accuracy')
plt.plot(epochs, Acc_val, 'b', label='validation accuracy')
plt.title('Training and Validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('accuracy')
plt.legend()
plt.show()
VGG19 model has given better results compared to basic CNN model. The model has given 98.87% accuracy in train set and 15.41% in test set.
ResNet50 model for classifying the car models
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
resnet_base_model = ResNet50(input_shape=(128, 128, 3),
weights='imagenet',
include_top=False)
x = resnet_base_model.output
x = GlobalAveragePooling2D()(x) # Optional
x = Dense(1024, activation='relu')(x) # Dense layer 1
x = Dropout(0.2)(x) # dropout
x = Dense(512, activation='relu')(x) # Dense layer 2
x = Dropout(0.2)(x) # Dropout
x = Dense(256, activation='relu')(x) # Dense layer 3
preds = Dense(196, activation='softmax')(x) #final layer with softmax activation
model_resnet50 = Model(inputs=resnet_base_model.input,
outputs=preds)
# set the first 20 layers of the network to be non-trainable
for layer in model_resnet50.layers[:20]:
layer.trainable=False
for layer in model_resnet50.layers[20:]:
layer.trainable=True
# Compile the model
model_resnet50.compile(optimizer='sgd',
loss='categorical_crossentropy',
metrics=['accuracy'])
model_resnet50.summary()
Model: "model_2"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_3 (InputLayer) [(None, 128, 128, 3 0 []
)]
conv1_pad (ZeroPadding2D) (None, 134, 134, 3) 0 ['input_3[0][0]']
conv1_conv (Conv2D) (None, 64, 64, 64) 9472 ['conv1_pad[0][0]']
conv1_bn (BatchNormalization) (None, 64, 64, 64) 256 ['conv1_conv[0][0]']
conv1_relu (Activation) (None, 64, 64, 64) 0 ['conv1_bn[0][0]']
pool1_pad (ZeroPadding2D) (None, 66, 66, 64) 0 ['conv1_relu[0][0]']
pool1_pool (MaxPooling2D) (None, 32, 32, 64) 0 ['pool1_pad[0][0]']
conv2_block1_1_conv (Conv2D) (None, 32, 32, 64) 4160 ['pool1_pool[0][0]']
conv2_block1_1_bn (BatchNormal (None, 32, 32, 64) 256 ['conv2_block1_1_conv[0][0]']
ization)
conv2_block1_1_relu (Activatio (None, 32, 32, 64) 0 ['conv2_block1_1_bn[0][0]']
n)
conv2_block1_2_conv (Conv2D) (None, 32, 32, 64) 36928 ['conv2_block1_1_relu[0][0]']
conv2_block1_2_bn (BatchNormal (None, 32, 32, 64) 256 ['conv2_block1_2_conv[0][0]']
ization)
conv2_block1_2_relu (Activatio (None, 32, 32, 64) 0 ['conv2_block1_2_bn[0][0]']
n)
conv2_block1_0_conv (Conv2D) (None, 32, 32, 256) 16640 ['pool1_pool[0][0]']
conv2_block1_3_conv (Conv2D) (None, 32, 32, 256) 16640 ['conv2_block1_2_relu[0][0]']
conv2_block1_0_bn (BatchNormal (None, 32, 32, 256) 1024 ['conv2_block1_0_conv[0][0]']
ization)
conv2_block1_3_bn (BatchNormal (None, 32, 32, 256) 1024 ['conv2_block1_3_conv[0][0]']
ization)
conv2_block1_add (Add) (None, 32, 32, 256) 0 ['conv2_block1_0_bn[0][0]',
'conv2_block1_3_bn[0][0]']
conv2_block1_out (Activation) (None, 32, 32, 256) 0 ['conv2_block1_add[0][0]']
conv2_block2_1_conv (Conv2D) (None, 32, 32, 64) 16448 ['conv2_block1_out[0][0]']
conv2_block2_1_bn (BatchNormal (None, 32, 32, 64) 256 ['conv2_block2_1_conv[0][0]']
ization)
conv2_block2_1_relu (Activatio (None, 32, 32, 64) 0 ['conv2_block2_1_bn[0][0]']
n)
conv2_block2_2_conv (Conv2D) (None, 32, 32, 64) 36928 ['conv2_block2_1_relu[0][0]']
conv2_block2_2_bn (BatchNormal (None, 32, 32, 64) 256 ['conv2_block2_2_conv[0][0]']
ization)
conv2_block2_2_relu (Activatio (None, 32, 32, 64) 0 ['conv2_block2_2_bn[0][0]']
n)
conv2_block2_3_conv (Conv2D) (None, 32, 32, 256) 16640 ['conv2_block2_2_relu[0][0]']
conv2_block2_3_bn (BatchNormal (None, 32, 32, 256) 1024 ['conv2_block2_3_conv[0][0]']
ization)
conv2_block2_add (Add) (None, 32, 32, 256) 0 ['conv2_block1_out[0][0]',
'conv2_block2_3_bn[0][0]']
conv2_block2_out (Activation) (None, 32, 32, 256) 0 ['conv2_block2_add[0][0]']
conv2_block3_1_conv (Conv2D) (None, 32, 32, 64) 16448 ['conv2_block2_out[0][0]']
conv2_block3_1_bn (BatchNormal (None, 32, 32, 64) 256 ['conv2_block3_1_conv[0][0]']
ization)
conv2_block3_1_relu (Activatio (None, 32, 32, 64) 0 ['conv2_block3_1_bn[0][0]']
n)
conv2_block3_2_conv (Conv2D) (None, 32, 32, 64) 36928 ['conv2_block3_1_relu[0][0]']
conv2_block3_2_bn (BatchNormal (None, 32, 32, 64) 256 ['conv2_block3_2_conv[0][0]']
ization)
conv2_block3_2_relu (Activatio (None, 32, 32, 64) 0 ['conv2_block3_2_bn[0][0]']
n)
conv2_block3_3_conv (Conv2D) (None, 32, 32, 256) 16640 ['conv2_block3_2_relu[0][0]']
conv2_block3_3_bn (BatchNormal (None, 32, 32, 256) 1024 ['conv2_block3_3_conv[0][0]']
ization)
conv2_block3_add (Add) (None, 32, 32, 256) 0 ['conv2_block2_out[0][0]',
'conv2_block3_3_bn[0][0]']
conv2_block3_out (Activation) (None, 32, 32, 256) 0 ['conv2_block3_add[0][0]']
conv3_block1_1_conv (Conv2D) (None, 16, 16, 128) 32896 ['conv2_block3_out[0][0]']
conv3_block1_1_bn (BatchNormal (None, 16, 16, 128) 512 ['conv3_block1_1_conv[0][0]']
ization)
conv3_block1_1_relu (Activatio (None, 16, 16, 128) 0 ['conv3_block1_1_bn[0][0]']
n)
conv3_block1_2_conv (Conv2D) (None, 16, 16, 128) 147584 ['conv3_block1_1_relu[0][0]']
conv3_block1_2_bn (BatchNormal (None, 16, 16, 128) 512 ['conv3_block1_2_conv[0][0]']
ization)
conv3_block1_2_relu (Activatio (None, 16, 16, 128) 0 ['conv3_block1_2_bn[0][0]']
n)
conv3_block1_0_conv (Conv2D) (None, 16, 16, 512) 131584 ['conv2_block3_out[0][0]']
conv3_block1_3_conv (Conv2D) (None, 16, 16, 512) 66048 ['conv3_block1_2_relu[0][0]']
conv3_block1_0_bn (BatchNormal (None, 16, 16, 512) 2048 ['conv3_block1_0_conv[0][0]']
ization)
conv3_block1_3_bn (BatchNormal (None, 16, 16, 512) 2048 ['conv3_block1_3_conv[0][0]']
ization)
conv3_block1_add (Add) (None, 16, 16, 512) 0 ['conv3_block1_0_bn[0][0]',
'conv3_block1_3_bn[0][0]']
conv3_block1_out (Activation) (None, 16, 16, 512) 0 ['conv3_block1_add[0][0]']
conv3_block2_1_conv (Conv2D) (None, 16, 16, 128) 65664 ['conv3_block1_out[0][0]']
conv3_block2_1_bn (BatchNormal (None, 16, 16, 128) 512 ['conv3_block2_1_conv[0][0]']
ization)
conv3_block2_1_relu (Activatio (None, 16, 16, 128) 0 ['conv3_block2_1_bn[0][0]']
n)
conv3_block2_2_conv (Conv2D) (None, 16, 16, 128) 147584 ['conv3_block2_1_relu[0][0]']
conv3_block2_2_bn (BatchNormal (None, 16, 16, 128) 512 ['conv3_block2_2_conv[0][0]']
ization)
conv3_block2_2_relu (Activatio (None, 16, 16, 128) 0 ['conv3_block2_2_bn[0][0]']
n)
conv3_block2_3_conv (Conv2D) (None, 16, 16, 512) 66048 ['conv3_block2_2_relu[0][0]']
conv3_block2_3_bn (BatchNormal (None, 16, 16, 512) 2048 ['conv3_block2_3_conv[0][0]']
ization)
conv3_block2_add (Add) (None, 16, 16, 512) 0 ['conv3_block1_out[0][0]',
'conv3_block2_3_bn[0][0]']
conv3_block2_out (Activation) (None, 16, 16, 512) 0 ['conv3_block2_add[0][0]']
conv3_block3_1_conv (Conv2D) (None, 16, 16, 128) 65664 ['conv3_block2_out[0][0]']
conv3_block3_1_bn (BatchNormal (None, 16, 16, 128) 512 ['conv3_block3_1_conv[0][0]']
ization)
conv3_block3_1_relu (Activatio (None, 16, 16, 128) 0 ['conv3_block3_1_bn[0][0]']
n)
conv3_block3_2_conv (Conv2D) (None, 16, 16, 128) 147584 ['conv3_block3_1_relu[0][0]']
conv3_block3_2_bn (BatchNormal (None, 16, 16, 128) 512 ['conv3_block3_2_conv[0][0]']
ization)
conv3_block3_2_relu (Activatio (None, 16, 16, 128) 0 ['conv3_block3_2_bn[0][0]']
n)
conv3_block3_3_conv (Conv2D) (None, 16, 16, 512) 66048 ['conv3_block3_2_relu[0][0]']
conv3_block3_3_bn (BatchNormal (None, 16, 16, 512) 2048 ['conv3_block3_3_conv[0][0]']
ization)
conv3_block3_add (Add) (None, 16, 16, 512) 0 ['conv3_block2_out[0][0]',
'conv3_block3_3_bn[0][0]']
conv3_block3_out (Activation) (None, 16, 16, 512) 0 ['conv3_block3_add[0][0]']
conv3_block4_1_conv (Conv2D) (None, 16, 16, 128) 65664 ['conv3_block3_out[0][0]']
conv3_block4_1_bn (BatchNormal (None, 16, 16, 128) 512 ['conv3_block4_1_conv[0][0]']
ization)
conv3_block4_1_relu (Activatio (None, 16, 16, 128) 0 ['conv3_block4_1_bn[0][0]']
n)
conv3_block4_2_conv (Conv2D) (None, 16, 16, 128) 147584 ['conv3_block4_1_relu[0][0]']
conv3_block4_2_bn (BatchNormal (None, 16, 16, 128) 512 ['conv3_block4_2_conv[0][0]']
ization)
conv3_block4_2_relu (Activatio (None, 16, 16, 128) 0 ['conv3_block4_2_bn[0][0]']
n)
conv3_block4_3_conv (Conv2D) (None, 16, 16, 512) 66048 ['conv3_block4_2_relu[0][0]']
conv3_block4_3_bn (BatchNormal (None, 16, 16, 512) 2048 ['conv3_block4_3_conv[0][0]']
ization)
conv3_block4_add (Add) (None, 16, 16, 512) 0 ['conv3_block3_out[0][0]',
'conv3_block4_3_bn[0][0]']
conv3_block4_out (Activation) (None, 16, 16, 512) 0 ['conv3_block4_add[0][0]']
conv4_block1_1_conv (Conv2D) (None, 8, 8, 256) 131328 ['conv3_block4_out[0][0]']
conv4_block1_1_bn (BatchNormal (None, 8, 8, 256) 1024 ['conv4_block1_1_conv[0][0]']
ization)
conv4_block1_1_relu (Activatio (None, 8, 8, 256) 0 ['conv4_block1_1_bn[0][0]']
n)
conv4_block1_2_conv (Conv2D) (None, 8, 8, 256) 590080 ['conv4_block1_1_relu[0][0]']
conv4_block1_2_bn (BatchNormal (None, 8, 8, 256) 1024 ['conv4_block1_2_conv[0][0]']
ization)
conv4_block1_2_relu (Activatio (None, 8, 8, 256) 0 ['conv4_block1_2_bn[0][0]']
n)
conv4_block1_0_conv (Conv2D) (None, 8, 8, 1024) 525312 ['conv3_block4_out[0][0]']
conv4_block1_3_conv (Conv2D) (None, 8, 8, 1024) 263168 ['conv4_block1_2_relu[0][0]']
conv4_block1_0_bn (BatchNormal (None, 8, 8, 1024) 4096 ['conv4_block1_0_conv[0][0]']
ization)
conv4_block1_3_bn (BatchNormal (None, 8, 8, 1024) 4096 ['conv4_block1_3_conv[0][0]']
ization)
conv4_block1_add (Add) (None, 8, 8, 1024) 0 ['conv4_block1_0_bn[0][0]',
'conv4_block1_3_bn[0][0]']
conv4_block1_out (Activation) (None, 8, 8, 1024) 0 ['conv4_block1_add[0][0]']
conv4_block2_1_conv (Conv2D) (None, 8, 8, 256) 262400 ['conv4_block1_out[0][0]']
conv4_block2_1_bn (BatchNormal (None, 8, 8, 256) 1024 ['conv4_block2_1_conv[0][0]']
ization)
conv4_block2_1_relu (Activatio (None, 8, 8, 256) 0 ['conv4_block2_1_bn[0][0]']
n)
conv4_block2_2_conv (Conv2D) (None, 8, 8, 256) 590080 ['conv4_block2_1_relu[0][0]']
conv4_block2_2_bn (BatchNormal (None, 8, 8, 256) 1024 ['conv4_block2_2_conv[0][0]']
ization)
conv4_block2_2_relu (Activatio (None, 8, 8, 256) 0 ['conv4_block2_2_bn[0][0]']
n)
conv4_block2_3_conv (Conv2D) (None, 8, 8, 1024) 263168 ['conv4_block2_2_relu[0][0]']
conv4_block2_3_bn (BatchNormal (None, 8, 8, 1024) 4096 ['conv4_block2_3_conv[0][0]']
ization)
conv4_block2_add (Add) (None, 8, 8, 1024) 0 ['conv4_block1_out[0][0]',
'conv4_block2_3_bn[0][0]']
conv4_block2_out (Activation) (None, 8, 8, 1024) 0 ['conv4_block2_add[0][0]']
conv4_block3_1_conv (Conv2D) (None, 8, 8, 256) 262400 ['conv4_block2_out[0][0]']
conv4_block3_1_bn (BatchNormal (None, 8, 8, 256) 1024 ['conv4_block3_1_conv[0][0]']
ization)
conv4_block3_1_relu (Activatio (None, 8, 8, 256) 0 ['conv4_block3_1_bn[0][0]']
n)
conv4_block3_2_conv (Conv2D) (None, 8, 8, 256) 590080 ['conv4_block3_1_relu[0][0]']
conv4_block3_2_bn (BatchNormal (None, 8, 8, 256) 1024 ['conv4_block3_2_conv[0][0]']
ization)
conv4_block3_2_relu (Activatio (None, 8, 8, 256) 0 ['conv4_block3_2_bn[0][0]']
n)
conv4_block3_3_conv (Conv2D) (None, 8, 8, 1024) 263168 ['conv4_block3_2_relu[0][0]']
conv4_block3_3_bn (BatchNormal (None, 8, 8, 1024) 4096 ['conv4_block3_3_conv[0][0]']
ization)
conv4_block3_add (Add) (None, 8, 8, 1024) 0 ['conv4_block2_out[0][0]',
'conv4_block3_3_bn[0][0]']
conv4_block3_out (Activation) (None, 8, 8, 1024) 0 ['conv4_block3_add[0][0]']
conv4_block4_1_conv (Conv2D) (None, 8, 8, 256) 262400 ['conv4_block3_out[0][0]']
conv4_block4_1_bn (BatchNormal (None, 8, 8, 256) 1024 ['conv4_block4_1_conv[0][0]']
ization)
conv4_block4_1_relu (Activatio (None, 8, 8, 256) 0 ['conv4_block4_1_bn[0][0]']
n)
conv4_block4_2_conv (Conv2D) (None, 8, 8, 256) 590080 ['conv4_block4_1_relu[0][0]']
conv4_block4_2_bn (BatchNormal (None, 8, 8, 256) 1024 ['conv4_block4_2_conv[0][0]']
ization)
conv4_block4_2_relu (Activatio (None, 8, 8, 256) 0 ['conv4_block4_2_bn[0][0]']
n)
conv4_block4_3_conv (Conv2D) (None, 8, 8, 1024) 263168 ['conv4_block4_2_relu[0][0]']
conv4_block4_3_bn (BatchNormal (None, 8, 8, 1024) 4096 ['conv4_block4_3_conv[0][0]']
ization)
conv4_block4_add (Add) (None, 8, 8, 1024) 0 ['conv4_block3_out[0][0]',
'conv4_block4_3_bn[0][0]']
conv4_block4_out (Activation) (None, 8, 8, 1024) 0 ['conv4_block4_add[0][0]']
conv4_block5_1_conv (Conv2D) (None, 8, 8, 256) 262400 ['conv4_block4_out[0][0]']
conv4_block5_1_bn (BatchNormal (None, 8, 8, 256) 1024 ['conv4_block5_1_conv[0][0]']
ization)
conv4_block5_1_relu (Activatio (None, 8, 8, 256) 0 ['conv4_block5_1_bn[0][0]']
n)
conv4_block5_2_conv (Conv2D) (None, 8, 8, 256) 590080 ['conv4_block5_1_relu[0][0]']
conv4_block5_2_bn (BatchNormal (None, 8, 8, 256) 1024 ['conv4_block5_2_conv[0][0]']
ization)
conv4_block5_2_relu (Activatio (None, 8, 8, 256) 0 ['conv4_block5_2_bn[0][0]']
n)
conv4_block5_3_conv (Conv2D) (None, 8, 8, 1024) 263168 ['conv4_block5_2_relu[0][0]']
conv4_block5_3_bn (BatchNormal (None, 8, 8, 1024) 4096 ['conv4_block5_3_conv[0][0]']
ization)
conv4_block5_add (Add) (None, 8, 8, 1024) 0 ['conv4_block4_out[0][0]',
'conv4_block5_3_bn[0][0]']
conv4_block5_out (Activation) (None, 8, 8, 1024) 0 ['conv4_block5_add[0][0]']
conv4_block6_1_conv (Conv2D) (None, 8, 8, 256) 262400 ['conv4_block5_out[0][0]']
conv4_block6_1_bn (BatchNormal (None, 8, 8, 256) 1024 ['conv4_block6_1_conv[0][0]']
ization)
conv4_block6_1_relu (Activatio (None, 8, 8, 256) 0 ['conv4_block6_1_bn[0][0]']
n)
conv4_block6_2_conv (Conv2D) (None, 8, 8, 256) 590080 ['conv4_block6_1_relu[0][0]']
conv4_block6_2_bn (BatchNormal (None, 8, 8, 256) 1024 ['conv4_block6_2_conv[0][0]']
ization)
conv4_block6_2_relu (Activatio (None, 8, 8, 256) 0 ['conv4_block6_2_bn[0][0]']
n)
conv4_block6_3_conv (Conv2D) (None, 8, 8, 1024) 263168 ['conv4_block6_2_relu[0][0]']
conv4_block6_3_bn (BatchNormal (None, 8, 8, 1024) 4096 ['conv4_block6_3_conv[0][0]']
ization)
conv4_block6_add (Add) (None, 8, 8, 1024) 0 ['conv4_block5_out[0][0]',
'conv4_block6_3_bn[0][0]']
conv4_block6_out (Activation) (None, 8, 8, 1024) 0 ['conv4_block6_add[0][0]']
conv5_block1_1_conv (Conv2D) (None, 4, 4, 512) 524800 ['conv4_block6_out[0][0]']
conv5_block1_1_bn (BatchNormal (None, 4, 4, 512) 2048 ['conv5_block1_1_conv[0][0]']
ization)
conv5_block1_1_relu (Activatio (None, 4, 4, 512) 0 ['conv5_block1_1_bn[0][0]']
n)
conv5_block1_2_conv (Conv2D) (None, 4, 4, 512) 2359808 ['conv5_block1_1_relu[0][0]']
conv5_block1_2_bn (BatchNormal (None, 4, 4, 512) 2048 ['conv5_block1_2_conv[0][0]']
ization)
conv5_block1_2_relu (Activatio (None, 4, 4, 512) 0 ['conv5_block1_2_bn[0][0]']
n)
conv5_block1_0_conv (Conv2D) (None, 4, 4, 2048) 2099200 ['conv4_block6_out[0][0]']
conv5_block1_3_conv (Conv2D) (None, 4, 4, 2048) 1050624 ['conv5_block1_2_relu[0][0]']
conv5_block1_0_bn (BatchNormal (None, 4, 4, 2048) 8192 ['conv5_block1_0_conv[0][0]']
ization)
conv5_block1_3_bn (BatchNormal (None, 4, 4, 2048) 8192 ['conv5_block1_3_conv[0][0]']
ization)
conv5_block1_add (Add) (None, 4, 4, 2048) 0 ['conv5_block1_0_bn[0][0]',
'conv5_block1_3_bn[0][0]']
conv5_block1_out (Activation) (None, 4, 4, 2048) 0 ['conv5_block1_add[0][0]']
conv5_block2_1_conv (Conv2D) (None, 4, 4, 512) 1049088 ['conv5_block1_out[0][0]']
conv5_block2_1_bn (BatchNormal (None, 4, 4, 512) 2048 ['conv5_block2_1_conv[0][0]']
ization)
conv5_block2_1_relu (Activatio (None, 4, 4, 512) 0 ['conv5_block2_1_bn[0][0]']
n)
conv5_block2_2_conv (Conv2D) (None, 4, 4, 512) 2359808 ['conv5_block2_1_relu[0][0]']
conv5_block2_2_bn (BatchNormal (None, 4, 4, 512) 2048 ['conv5_block2_2_conv[0][0]']
ization)
conv5_block2_2_relu (Activatio (None, 4, 4, 512) 0 ['conv5_block2_2_bn[0][0]']
n)
conv5_block2_3_conv (Conv2D) (None, 4, 4, 2048) 1050624 ['conv5_block2_2_relu[0][0]']
conv5_block2_3_bn (BatchNormal (None, 4, 4, 2048) 8192 ['conv5_block2_3_conv[0][0]']
ization)
conv5_block2_add (Add) (None, 4, 4, 2048) 0 ['conv5_block1_out[0][0]',
'conv5_block2_3_bn[0][0]']
conv5_block2_out (Activation) (None, 4, 4, 2048) 0 ['conv5_block2_add[0][0]']
conv5_block3_1_conv (Conv2D) (None, 4, 4, 512) 1049088 ['conv5_block2_out[0][0]']
conv5_block3_1_bn (BatchNormal (None, 4, 4, 512) 2048 ['conv5_block3_1_conv[0][0]']
ization)
conv5_block3_1_relu (Activatio (None, 4, 4, 512) 0 ['conv5_block3_1_bn[0][0]']
n)
conv5_block3_2_conv (Conv2D) (None, 4, 4, 512) 2359808 ['conv5_block3_1_relu[0][0]']
conv5_block3_2_bn (BatchNormal (None, 4, 4, 512) 2048 ['conv5_block3_2_conv[0][0]']
ization)
conv5_block3_2_relu (Activatio (None, 4, 4, 512) 0 ['conv5_block3_2_bn[0][0]']
n)
conv5_block3_3_conv (Conv2D) (None, 4, 4, 2048) 1050624 ['conv5_block3_2_relu[0][0]']
conv5_block3_3_bn (BatchNormal (None, 4, 4, 2048) 8192 ['conv5_block3_3_conv[0][0]']
ization)
conv5_block3_add (Add) (None, 4, 4, 2048) 0 ['conv5_block2_out[0][0]',
'conv5_block3_3_bn[0][0]']
conv5_block3_out (Activation) (None, 4, 4, 2048) 0 ['conv5_block3_add[0][0]']
global_average_pooling2d_2 (Gl (None, 2048) 0 ['conv5_block3_out[0][0]']
obalAveragePooling2D)
dense_11 (Dense) (None, 1024) 2098176 ['global_average_pooling2d_2[0][0
]']
dropout_6 (Dropout) (None, 1024) 0 ['dense_11[0][0]']
dense_12 (Dense) (None, 512) 524800 ['dropout_6[0][0]']
dropout_7 (Dropout) (None, 512) 0 ['dense_12[0][0]']
dense_13 (Dense) (None, 256) 131328 ['dropout_7[0][0]']
dense_14 (Dense) (None, 196) 50372 ['dense_13[0][0]']
==================================================================================================
Total params: 26,392,388
Trainable params: 26,237,572
Non-trainable params: 154,816
__________________________________________________________________________________________________
resnet50 = model_resnet50.fit(X_train, y_train_encoded, validation_data=(X_test, y_test_encoded),callbacks=[reduce_lr, stop], epochs=100, verbose=1)
Epoch 1/100 255/255 [==============================] - 44s 153ms/step - loss: 5.3102 - accuracy: 0.0055 - val_loss: 5.2865 - val_accuracy: 0.0047 - lr: 0.0100 Epoch 2/100 255/255 [==============================] - 37s 144ms/step - loss: 5.2810 - accuracy: 0.0060 - val_loss: 5.2772 - val_accuracy: 0.0060 - lr: 0.0100 Epoch 3/100 255/255 [==============================] - 37s 145ms/step - loss: 5.2707 - accuracy: 0.0074 - val_loss: 5.2710 - val_accuracy: 0.0083 - lr: 0.0100 Epoch 4/100 255/255 [==============================] - 37s 145ms/step - loss: 5.2510 - accuracy: 0.0076 - val_loss: 5.2541 - val_accuracy: 0.0112 - lr: 0.0100 Epoch 5/100 255/255 [==============================] - 37s 145ms/step - loss: 5.2171 - accuracy: 0.0144 - val_loss: 5.2174 - val_accuracy: 0.0167 - lr: 0.0100 Epoch 6/100 255/255 [==============================] - 37s 145ms/step - loss: 5.1423 - accuracy: 0.0205 - val_loss: 5.1301 - val_accuracy: 0.0224 - lr: 0.0100 Epoch 7/100 255/255 [==============================] - 37s 145ms/step - loss: 4.9839 - accuracy: 0.0341 - val_loss: 5.0053 - val_accuracy: 0.0272 - lr: 0.0100 Epoch 8/100 255/255 [==============================] - 37s 144ms/step - loss: 4.7322 - accuracy: 0.0458 - val_loss: 4.9628 - val_accuracy: 0.0305 - lr: 0.0100 Epoch 9/100 255/255 [==============================] - 37s 145ms/step - loss: 4.4486 - accuracy: 0.0685 - val_loss: 4.6267 - val_accuracy: 0.0500 - lr: 0.0100 Epoch 10/100 255/255 [==============================] - 37s 145ms/step - loss: 4.1316 - accuracy: 0.1004 - val_loss: 4.8238 - val_accuracy: 0.0501 - lr: 0.0100 Epoch 11/100 255/255 [==============================] - 37s 145ms/step - loss: 3.8081 - accuracy: 0.1335 - val_loss: 4.4288 - val_accuracy: 0.0629 - lr: 0.0100 Epoch 12/100 255/255 [==============================] - 37s 145ms/step - loss: 3.4471 - accuracy: 0.1805 - val_loss: 4.1668 - val_accuracy: 0.0920 - lr: 0.0100 Epoch 13/100 255/255 [==============================] - 37s 145ms/step - loss: 3.0776 - accuracy: 0.2448 - val_loss: 4.3344 - val_accuracy: 0.0907 - lr: 0.0100 Epoch 14/100 255/255 [==============================] - 37s 145ms/step - loss: 2.7272 - accuracy: 0.3021 - val_loss: 4.3710 - val_accuracy: 0.1068 - lr: 0.0100 Epoch 15/100 255/255 [==============================] - 37s 145ms/step - loss: 2.3371 - accuracy: 0.3841 - val_loss: 5.4837 - val_accuracy: 0.0818 - lr: 0.0100 Epoch 16/100 255/255 [==============================] - 37s 145ms/step - loss: 1.9502 - accuracy: 0.4700 - val_loss: 4.9608 - val_accuracy: 0.0919 - lr: 0.0100 Epoch 17/100 255/255 [==============================] - 37s 145ms/step - loss: 1.6262 - accuracy: 0.5456 - val_loss: 5.8848 - val_accuracy: 0.0685 - lr: 0.0100 Epoch 18/100 255/255 [==============================] - 37s 145ms/step - loss: 1.2892 - accuracy: 0.6324 - val_loss: 5.3170 - val_accuracy: 0.1229 - lr: 0.0100 Epoch 19/100 255/255 [==============================] - 37s 145ms/step - loss: 1.0235 - accuracy: 0.6989 - val_loss: 5.2595 - val_accuracy: 0.1287 - lr: 0.0100 Epoch 20/100 255/255 [==============================] - 37s 145ms/step - loss: 0.8030 - accuracy: 0.7626 - val_loss: 5.5612 - val_accuracy: 0.1313 - lr: 0.0100 Epoch 21/100 255/255 [==============================] - 37s 145ms/step - loss: 0.6103 - accuracy: 0.8193 - val_loss: 5.9346 - val_accuracy: 0.1348 - lr: 0.0100 Epoch 22/100 255/255 [==============================] - 37s 144ms/step - loss: 0.4928 - accuracy: 0.8506 - val_loss: 5.7339 - val_accuracy: 0.1338 - lr: 0.0100 Epoch 23/100 255/255 [==============================] - 37s 144ms/step - loss: 0.3851 - accuracy: 0.8897 - val_loss: 5.7591 - val_accuracy: 0.1405 - lr: 0.0100 Epoch 24/100 255/255 [==============================] - 37s 145ms/step - loss: 0.2797 - accuracy: 0.9164 - val_loss: 5.6964 - val_accuracy: 0.1757 - lr: 0.0100 Epoch 25/100 255/255 [==============================] - 37s 145ms/step - loss: 0.2236 - accuracy: 0.9342 - val_loss: 6.3289 - val_accuracy: 0.1491 - lr: 0.0100 Epoch 26/100 255/255 [==============================] - 37s 145ms/step - loss: 0.1832 - accuracy: 0.9465 - val_loss: 5.9232 - val_accuracy: 0.1781 - lr: 0.0100 Epoch 27/100 255/255 [==============================] - 37s 145ms/step - loss: 0.1650 - accuracy: 0.9538 - val_loss: 6.7764 - val_accuracy: 0.1573 - lr: 0.0100 Epoch 28/100 255/255 [==============================] - 37s 145ms/step - loss: 0.1396 - accuracy: 0.9587 - val_loss: 5.6919 - val_accuracy: 0.1975 - lr: 0.0100 Epoch 29/100 255/255 [==============================] - 37s 145ms/step - loss: 0.1137 - accuracy: 0.9681 - val_loss: 6.1654 - val_accuracy: 0.1863 - lr: 0.0100 Epoch 30/100 255/255 [==============================] - 37s 145ms/step - loss: 0.1092 - accuracy: 0.9687 - val_loss: 6.1437 - val_accuracy: 0.1791 - lr: 0.0100 Epoch 31/100 255/255 [==============================] - 37s 144ms/step - loss: 0.0890 - accuracy: 0.9747 - val_loss: 6.3536 - val_accuracy: 0.1726 - lr: 0.0100 Epoch 32/100 255/255 [==============================] - 37s 145ms/step - loss: 0.0786 - accuracy: 0.9777 - val_loss: 5.9598 - val_accuracy: 0.2083 - lr: 0.0100 Epoch 33/100 255/255 [==============================] - 37s 145ms/step - loss: 0.0653 - accuracy: 0.9805 - val_loss: 5.9366 - val_accuracy: 0.1979 - lr: 0.0100 Epoch 34/100 255/255 [==============================] - 37s 144ms/step - loss: 0.0574 - accuracy: 0.9850 - val_loss: 6.4843 - val_accuracy: 0.1841 - lr: 0.0100 Epoch 35/100 255/255 [==============================] - 37s 145ms/step - loss: 0.0486 - accuracy: 0.9859 - val_loss: 6.2278 - val_accuracy: 0.2008 - lr: 0.0100 Epoch 36/100 255/255 [==============================] - 37s 144ms/step - loss: 0.0508 - accuracy: 0.9860 - val_loss: 6.0836 - val_accuracy: 0.2069 - lr: 0.0100 Epoch 37/100 255/255 [==============================] - 37s 144ms/step - loss: 0.0493 - accuracy: 0.9864 - val_loss: 5.9826 - val_accuracy: 0.2107 - lr: 0.0100 Epoch 38/100 255/255 [==============================] - 37s 145ms/step - loss: 0.0485 - accuracy: 0.9856 - val_loss: 6.5744 - val_accuracy: 0.1847 - lr: 0.0100 Epoch 39/100 255/255 [==============================] - 37s 145ms/step - loss: 0.0451 - accuracy: 0.9865 - val_loss: 6.0458 - val_accuracy: 0.2129 - lr: 0.0100 Epoch 40/100 255/255 [==============================] - 37s 145ms/step - loss: 0.0332 - accuracy: 0.9913 - val_loss: 5.9233 - val_accuracy: 0.2212 - lr: 0.0100 Epoch 41/100 255/255 [==============================] - 37s 145ms/step - loss: 0.0240 - accuracy: 0.9937 - val_loss: 6.1395 - val_accuracy: 0.2217 - lr: 0.0100 Epoch 42/100 255/255 [==============================] - 37s 144ms/step - loss: 0.0201 - accuracy: 0.9944 - val_loss: 6.0499 - val_accuracy: 0.2276 - lr: 0.0100 Epoch 43/100 255/255 [==============================] - 37s 145ms/step - loss: 0.0208 - accuracy: 0.9947 - val_loss: 6.4009 - val_accuracy: 0.2179 - lr: 0.0100 Epoch 44/100 255/255 [==============================] - 37s 145ms/step - loss: 0.0256 - accuracy: 0.9929 - val_loss: 6.2649 - val_accuracy: 0.2135 - lr: 0.0100 Epoch 45/100 255/255 [==============================] - 37s 145ms/step - loss: 0.0253 - accuracy: 0.9914 - val_loss: 6.5825 - val_accuracy: 0.2092 - lr: 0.0100 Epoch 46/100 255/255 [==============================] - 37s 145ms/step - loss: 0.0284 - accuracy: 0.9912 - val_loss: 6.3384 - val_accuracy: 0.2127 - lr: 0.0100 Epoch 47/100 255/255 [==============================] - ETA: 0s - loss: 0.0217 - accuracy: 0.9942 Epoch 47: ReduceLROnPlateau reducing learning rate to 0.0019999999552965165. 255/255 [==============================] - 37s 145ms/step - loss: 0.0217 - accuracy: 0.9942 - val_loss: 6.4496 - val_accuracy: 0.2129 - lr: 0.0100
## Plotting Train and Test loss
epoch=47
loss_train = resnet50.history['loss']
loss_val = resnet50.history['val_loss']
epochs = range(1,epoch+1)
plt.plot(epochs, loss_train, 'g', label='Training loss')
plt.plot(epochs, loss_val, 'b', label='validation loss')
plt.title('Training and Validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
## Plotting Train and Test accuracy
Acc_train = resnet50.history['accuracy']
Acc_val = resnet50.history['val_accuracy']
epochs = range(1,epoch+1)
plt.plot(epochs, Acc_train, 'g', label='Training accuracy')
plt.plot(epochs, Acc_val, 'b', label='validation accuracy')
plt.title('Training and Validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('accuracy')
plt.legend()
plt.show()
MobileNet model for classifying the car models
from tensorflow.keras.applications import MobileNet
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
mobilenet_base_model = MobileNet(input_shape=X_train[0].shape,
weights='imagenet',
include_top=False)
x = mobilenet_base_model.output
x = GlobalAveragePooling2D()(x) # Optional
x = Dense(1024,activation='relu')(x) # Dense layer 1
x = Dropout(0.2)(x) # Dropout
x = Dense(512,activation='relu')(x) # Dense layer 2
x = Dropout(0.2)(x) # Dropout
x = Dense(256,activation='relu')(x) # Dense layer 3
preds = Dense(196,activation='softmax')(x) # final layer with softmax activation
model_mobilenet = Model(inputs=mobilenet_base_model.input,
outputs=preds)
# set the first 20 layers of the network to be non-trainable
for layer in model_mobilenet.layers[:20]:
layer.trainable=False
for layer in model_mobilenet.layers[20:]:
layer.trainable=True
# Compile the model
opt = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.001, amsgrad=False)
model_mobilenet.compile(optimizer=opt,
loss='categorical_crossentropy',
metrics=['accuracy'])
model_mobilenet.summary()
Model: "model_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_4 (InputLayer) [(None, 128, 128, 3)] 0
conv1 (Conv2D) (None, 64, 64, 32) 864
conv1_bn (BatchNormalizatio (None, 64, 64, 32) 128
n)
conv1_relu (ReLU) (None, 64, 64, 32) 0
conv_dw_1 (DepthwiseConv2D) (None, 64, 64, 32) 288
conv_dw_1_bn (BatchNormaliz (None, 64, 64, 32) 128
ation)
conv_dw_1_relu (ReLU) (None, 64, 64, 32) 0
conv_pw_1 (Conv2D) (None, 64, 64, 64) 2048
conv_pw_1_bn (BatchNormaliz (None, 64, 64, 64) 256
ation)
conv_pw_1_relu (ReLU) (None, 64, 64, 64) 0
conv_pad_2 (ZeroPadding2D) (None, 65, 65, 64) 0
conv_dw_2 (DepthwiseConv2D) (None, 32, 32, 64) 576
conv_dw_2_bn (BatchNormaliz (None, 32, 32, 64) 256
ation)
conv_dw_2_relu (ReLU) (None, 32, 32, 64) 0
conv_pw_2 (Conv2D) (None, 32, 32, 128) 8192
conv_pw_2_bn (BatchNormaliz (None, 32, 32, 128) 512
ation)
conv_pw_2_relu (ReLU) (None, 32, 32, 128) 0
conv_dw_3 (DepthwiseConv2D) (None, 32, 32, 128) 1152
conv_dw_3_bn (BatchNormaliz (None, 32, 32, 128) 512
ation)
conv_dw_3_relu (ReLU) (None, 32, 32, 128) 0
conv_pw_3 (Conv2D) (None, 32, 32, 128) 16384
conv_pw_3_bn (BatchNormaliz (None, 32, 32, 128) 512
ation)
conv_pw_3_relu (ReLU) (None, 32, 32, 128) 0
conv_pad_4 (ZeroPadding2D) (None, 33, 33, 128) 0
conv_dw_4 (DepthwiseConv2D) (None, 16, 16, 128) 1152
conv_dw_4_bn (BatchNormaliz (None, 16, 16, 128) 512
ation)
conv_dw_4_relu (ReLU) (None, 16, 16, 128) 0
conv_pw_4 (Conv2D) (None, 16, 16, 256) 32768
conv_pw_4_bn (BatchNormaliz (None, 16, 16, 256) 1024
ation)
conv_pw_4_relu (ReLU) (None, 16, 16, 256) 0
conv_dw_5 (DepthwiseConv2D) (None, 16, 16, 256) 2304
conv_dw_5_bn (BatchNormaliz (None, 16, 16, 256) 1024
ation)
conv_dw_5_relu (ReLU) (None, 16, 16, 256) 0
conv_pw_5 (Conv2D) (None, 16, 16, 256) 65536
conv_pw_5_bn (BatchNormaliz (None, 16, 16, 256) 1024
ation)
conv_pw_5_relu (ReLU) (None, 16, 16, 256) 0
conv_pad_6 (ZeroPadding2D) (None, 17, 17, 256) 0
conv_dw_6 (DepthwiseConv2D) (None, 8, 8, 256) 2304
conv_dw_6_bn (BatchNormaliz (None, 8, 8, 256) 1024
ation)
conv_dw_6_relu (ReLU) (None, 8, 8, 256) 0
conv_pw_6 (Conv2D) (None, 8, 8, 512) 131072
conv_pw_6_bn (BatchNormaliz (None, 8, 8, 512) 2048
ation)
conv_pw_6_relu (ReLU) (None, 8, 8, 512) 0
conv_dw_7 (DepthwiseConv2D) (None, 8, 8, 512) 4608
conv_dw_7_bn (BatchNormaliz (None, 8, 8, 512) 2048
ation)
conv_dw_7_relu (ReLU) (None, 8, 8, 512) 0
conv_pw_7 (Conv2D) (None, 8, 8, 512) 262144
conv_pw_7_bn (BatchNormaliz (None, 8, 8, 512) 2048
ation)
/usr/local/lib/python3.8/dist-packages/keras/optimizers/optimizer_v2/adam.py:110: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead. super(Adam, self).__init__(name, **kwargs)
conv_pw_7_relu (ReLU) (None, 8, 8, 512) 0
conv_dw_8 (DepthwiseConv2D) (None, 8, 8, 512) 4608
conv_dw_8_bn (BatchNormaliz (None, 8, 8, 512) 2048
ation)
conv_dw_8_relu (ReLU) (None, 8, 8, 512) 0
conv_pw_8 (Conv2D) (None, 8, 8, 512) 262144
conv_pw_8_bn (BatchNormaliz (None, 8, 8, 512) 2048
ation)
conv_pw_8_relu (ReLU) (None, 8, 8, 512) 0
conv_dw_9 (DepthwiseConv2D) (None, 8, 8, 512) 4608
conv_dw_9_bn (BatchNormaliz (None, 8, 8, 512) 2048
ation)
conv_dw_9_relu (ReLU) (None, 8, 8, 512) 0
conv_pw_9 (Conv2D) (None, 8, 8, 512) 262144
conv_pw_9_bn (BatchNormaliz (None, 8, 8, 512) 2048
ation)
conv_pw_9_relu (ReLU) (None, 8, 8, 512) 0
conv_dw_10 (DepthwiseConv2D (None, 8, 8, 512) 4608
)
conv_dw_10_bn (BatchNormali (None, 8, 8, 512) 2048
zation)
conv_dw_10_relu (ReLU) (None, 8, 8, 512) 0
conv_pw_10 (Conv2D) (None, 8, 8, 512) 262144
conv_pw_10_bn (BatchNormali (None, 8, 8, 512) 2048
zation)
conv_pw_10_relu (ReLU) (None, 8, 8, 512) 0
conv_dw_11 (DepthwiseConv2D (None, 8, 8, 512) 4608
)
conv_dw_11_bn (BatchNormali (None, 8, 8, 512) 2048
zation)
conv_dw_11_relu (ReLU) (None, 8, 8, 512) 0
conv_pw_11 (Conv2D) (None, 8, 8, 512) 262144
conv_pw_11_bn (BatchNormali (None, 8, 8, 512) 2048
zation)
conv_pw_11_relu (ReLU) (None, 8, 8, 512) 0
conv_pad_12 (ZeroPadding2D) (None, 9, 9, 512) 0
conv_dw_12 (DepthwiseConv2D (None, 4, 4, 512) 4608
)
conv_dw_12_bn (BatchNormali (None, 4, 4, 512) 2048
zation)
conv_dw_12_relu (ReLU) (None, 4, 4, 512) 0
conv_pw_12 (Conv2D) (None, 4, 4, 1024) 524288
conv_pw_12_bn (BatchNormali (None, 4, 4, 1024) 4096
zation)
conv_pw_12_relu (ReLU) (None, 4, 4, 1024) 0
conv_dw_13 (DepthwiseConv2D (None, 4, 4, 1024) 9216
)
conv_dw_13_bn (BatchNormali (None, 4, 4, 1024) 4096
zation)
conv_dw_13_relu (ReLU) (None, 4, 4, 1024) 0
conv_pw_13 (Conv2D) (None, 4, 4, 1024) 1048576
conv_pw_13_bn (BatchNormali (None, 4, 4, 1024) 4096
zation)
conv_pw_13_relu (ReLU) (None, 4, 4, 1024) 0
global_average_pooling2d_3 (None, 1024) 0
(GlobalAveragePooling2D)
dense_15 (Dense) (None, 1024) 1049600
dropout_8 (Dropout) (None, 1024) 0
dense_16 (Dense) (None, 512) 524800
dropout_9 (Dropout) (None, 512) 0
dense_17 (Dense) (None, 256) 131328
dense_18 (Dense) (None, 196) 50372
=================================================================
Total params: 4,984,964
Trainable params: 4,949,060
Non-trainable params: 35,904
_________________________________________________________________
mobilenet = model_mobilenet.fit(X_train, y_train_encoded, validation_data=(X_test, y_test_encoded), callbacks=[reduce_lr, stop], epochs=200, verbose=1)
Epoch 1/200 255/255 [==============================] - 16s 51ms/step - loss: 5.0593 - accuracy: 0.0172 - val_loss: 5.0251 - val_accuracy: 0.0363 - lr: 0.0010 Epoch 2/200 255/255 [==============================] - 11s 44ms/step - loss: 4.1041 - accuracy: 0.0657 - val_loss: 3.9427 - val_accuracy: 0.0874 - lr: 0.0010 Epoch 3/200 255/255 [==============================] - 11s 44ms/step - loss: 3.3450 - accuracy: 0.1465 - val_loss: 3.3056 - val_accuracy: 0.1701 - lr: 0.0010 Epoch 4/200 255/255 [==============================] - 11s 44ms/step - loss: 2.6825 - accuracy: 0.2514 - val_loss: 2.8594 - val_accuracy: 0.2438 - lr: 0.0010 Epoch 5/200 255/255 [==============================] - 11s 44ms/step - loss: 2.1371 - accuracy: 0.3681 - val_loss: 2.9775 - val_accuracy: 0.2470 - lr: 0.0010 Epoch 6/200 255/255 [==============================] - 11s 44ms/step - loss: 1.6491 - accuracy: 0.4790 - val_loss: 2.6554 - val_accuracy: 0.3253 - lr: 0.0010 Epoch 7/200 255/255 [==============================] - 11s 44ms/step - loss: 1.2943 - accuracy: 0.5833 - val_loss: 2.5622 - val_accuracy: 0.3616 - lr: 0.0010 Epoch 8/200 255/255 [==============================] - 11s 44ms/step - loss: 0.9892 - accuracy: 0.6756 - val_loss: 2.3816 - val_accuracy: 0.4228 - lr: 0.0010 Epoch 9/200 255/255 [==============================] - 11s 44ms/step - loss: 0.7533 - accuracy: 0.7526 - val_loss: 2.6593 - val_accuracy: 0.4092 - lr: 0.0010 Epoch 10/200 255/255 [==============================] - 11s 44ms/step - loss: 0.5861 - accuracy: 0.8028 - val_loss: 2.6717 - val_accuracy: 0.4228 - lr: 0.0010 Epoch 11/200 255/255 [==============================] - 11s 44ms/step - loss: 0.4765 - accuracy: 0.8379 - val_loss: 2.7531 - val_accuracy: 0.4271 - lr: 0.0010 Epoch 12/200 255/255 [==============================] - 11s 44ms/step - loss: 0.3483 - accuracy: 0.8868 - val_loss: 3.0667 - val_accuracy: 0.4166 - lr: 0.0010 Epoch 13/200 255/255 [==============================] - 11s 44ms/step - loss: 0.2825 - accuracy: 0.9100 - val_loss: 2.8510 - val_accuracy: 0.4543 - lr: 0.0010 Epoch 14/200 255/255 [==============================] - 11s 44ms/step - loss: 0.2488 - accuracy: 0.9187 - val_loss: 2.9778 - val_accuracy: 0.4567 - lr: 0.0010 Epoch 15/200 255/255 [==============================] - 11s 44ms/step - loss: 0.1937 - accuracy: 0.9373 - val_loss: 3.1271 - val_accuracy: 0.4536 - lr: 0.0010 Epoch 16/200 255/255 [==============================] - 11s 44ms/step - loss: 0.1722 - accuracy: 0.9445 - val_loss: 3.1693 - val_accuracy: 0.4619 - lr: 0.0010 Epoch 17/200 255/255 [==============================] - 11s 44ms/step - loss: 0.1533 - accuracy: 0.9468 - val_loss: 3.1512 - val_accuracy: 0.4702 - lr: 0.0010 Epoch 18/200 255/255 [==============================] - 11s 44ms/step - loss: 0.1260 - accuracy: 0.9583 - val_loss: 3.1434 - val_accuracy: 0.4746 - lr: 0.0010 Epoch 19/200 255/255 [==============================] - 11s 44ms/step - loss: 0.1215 - accuracy: 0.9612 - val_loss: 3.1537 - val_accuracy: 0.4784 - lr: 0.0010 Epoch 20/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0979 - accuracy: 0.9692 - val_loss: 3.2044 - val_accuracy: 0.4846 - lr: 0.0010 Epoch 21/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0912 - accuracy: 0.9703 - val_loss: 3.2645 - val_accuracy: 0.4915 - lr: 0.0010 Epoch 22/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0841 - accuracy: 0.9748 - val_loss: 3.1620 - val_accuracy: 0.4981 - lr: 0.0010 Epoch 23/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0847 - accuracy: 0.9738 - val_loss: 3.3475 - val_accuracy: 0.4697 - lr: 0.0010 Epoch 24/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0709 - accuracy: 0.9781 - val_loss: 3.2221 - val_accuracy: 0.4940 - lr: 0.0010 Epoch 25/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0621 - accuracy: 0.9797 - val_loss: 3.3219 - val_accuracy: 0.4890 - lr: 0.0010 Epoch 26/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0676 - accuracy: 0.9794 - val_loss: 3.2262 - val_accuracy: 0.4911 - lr: 0.0010 Epoch 27/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0556 - accuracy: 0.9832 - val_loss: 3.3583 - val_accuracy: 0.4892 - lr: 0.0010 Epoch 28/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0625 - accuracy: 0.9796 - val_loss: 3.6237 - val_accuracy: 0.4779 - lr: 0.0010 Epoch 29/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0604 - accuracy: 0.9807 - val_loss: 3.4092 - val_accuracy: 0.4910 - lr: 0.0010 Epoch 30/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0506 - accuracy: 0.9843 - val_loss: 3.5012 - val_accuracy: 0.4937 - lr: 0.0010 Epoch 31/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0468 - accuracy: 0.9855 - val_loss: 3.3898 - val_accuracy: 0.4984 - lr: 0.0010 Epoch 32/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0460 - accuracy: 0.9835 - val_loss: 3.5779 - val_accuracy: 0.4848 - lr: 0.0010 Epoch 33/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0469 - accuracy: 0.9861 - val_loss: 3.4611 - val_accuracy: 0.4962 - lr: 0.0010 Epoch 34/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0477 - accuracy: 0.9843 - val_loss: 3.3861 - val_accuracy: 0.5044 - lr: 0.0010 Epoch 35/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0374 - accuracy: 0.9878 - val_loss: 3.4322 - val_accuracy: 0.5023 - lr: 0.0010 Epoch 36/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0354 - accuracy: 0.9881 - val_loss: 3.4302 - val_accuracy: 0.5037 - lr: 0.0010 Epoch 37/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0311 - accuracy: 0.9894 - val_loss: 3.5335 - val_accuracy: 0.4997 - lr: 0.0010 Epoch 38/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0464 - accuracy: 0.9853 - val_loss: 3.4221 - val_accuracy: 0.4950 - lr: 0.0010 Epoch 39/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0420 - accuracy: 0.9869 - val_loss: 3.5105 - val_accuracy: 0.4998 - lr: 0.0010 Epoch 40/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0334 - accuracy: 0.9887 - val_loss: 3.2892 - val_accuracy: 0.5146 - lr: 0.0010 Epoch 41/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0275 - accuracy: 0.9921 - val_loss: 3.4040 - val_accuracy: 0.5030 - lr: 0.0010 Epoch 42/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0251 - accuracy: 0.9921 - val_loss: 3.4929 - val_accuracy: 0.5040 - lr: 0.0010 Epoch 43/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0216 - accuracy: 0.9935 - val_loss: 3.5145 - val_accuracy: 0.5034 - lr: 0.0010 Epoch 44/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0216 - accuracy: 0.9936 - val_loss: 3.4849 - val_accuracy: 0.5098 - lr: 0.0010 Epoch 45/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0222 - accuracy: 0.9929 - val_loss: 3.6214 - val_accuracy: 0.5048 - lr: 0.0010 Epoch 46/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0288 - accuracy: 0.9905 - val_loss: 3.6417 - val_accuracy: 0.4986 - lr: 0.0010 Epoch 47/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0308 - accuracy: 0.9899 - val_loss: 3.6946 - val_accuracy: 0.4941 - lr: 0.0010 Epoch 48/200 255/255 [==============================] - ETA: 0s - loss: 0.0241 - accuracy: 0.9919 Epoch 48: ReduceLROnPlateau reducing learning rate to 0.00020000000949949026. 255/255 [==============================] - 11s 44ms/step - loss: 0.0241 - accuracy: 0.9919 - val_loss: 3.5491 - val_accuracy: 0.5090 - lr: 0.0010 Epoch 49/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0163 - accuracy: 0.9935 - val_loss: 3.4696 - val_accuracy: 0.5142 - lr: 2.0000e-04 Epoch 50/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0137 - accuracy: 0.9958 - val_loss: 3.4588 - val_accuracy: 0.5171 - lr: 2.0000e-04 Epoch 51/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0124 - accuracy: 0.9956 - val_loss: 3.4513 - val_accuracy: 0.5180 - lr: 2.0000e-04 Epoch 52/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0089 - accuracy: 0.9971 - val_loss: 3.4495 - val_accuracy: 0.5208 - lr: 2.0000e-04 Epoch 53/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0101 - accuracy: 0.9969 - val_loss: 3.4527 - val_accuracy: 0.5217 - lr: 2.0000e-04 Epoch 54/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0108 - accuracy: 0.9958 - val_loss: 3.4328 - val_accuracy: 0.5203 - lr: 2.0000e-04 Epoch 55/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0133 - accuracy: 0.9947 - val_loss: 3.4592 - val_accuracy: 0.5217 - lr: 2.0000e-04 Epoch 56/200 255/255 [==============================] - 11s 44ms/step - loss: 0.0106 - accuracy: 0.9953 - val_loss: 3.4583 - val_accuracy: 0.5211 - lr: 2.0000e-04 Epoch 57/200 255/255 [==============================] - ETA: 0s - loss: 0.0089 - accuracy: 0.9982 Epoch 57: ReduceLROnPlateau reducing learning rate to 4.0000001899898055e-05. 255/255 [==============================] - 11s 44ms/step - loss: 0.0089 - accuracy: 0.9982 - val_loss: 3.4674 - val_accuracy: 0.5227 - lr: 2.0000e-04
## Plotting Train and Test loss
epoch=57
loss_train = mobilenet.history['loss']
loss_val = mobilenet.history['val_loss']
epochs = range(1,epoch+1)
plt.plot(epochs, loss_train, 'g', label='Training loss')
plt.plot(epochs, loss_val, 'b', label='validation loss')
plt.title('Training and Validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
## Plotting Train and Test accuracy
Acc_train = mobilenet.history['accuracy']
Acc_val = mobilenet.history['val_accuracy']
epochs = range(1,epoch+1)
plt.plot(epochs, Acc_train, 'g', label='Training accuracy')
plt.plot(epochs, Acc_val, 'b', label='validation accuracy')
plt.title('Training and Validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('accuracy')
plt.legend()
plt.show()
Of all the models tried above, MobileNet model has given best results. The model has produced 99.82% accuracy in train set and 52.27% accuracy in test set.
from matplotlib import pyplot as plt
def predict_images(images_list,n):
rand = np.random.randint(0, len(images_list), n) # Generating n random numbers from total number of images
print("Showing images with index in : {}".format(rand))
plt.figure(figsize=(10, 10))
for cnt,j in enumerate(rand):
plt.subplot(n, 2, (cnt*2)+1)
plt.title("Actual : {}".format(images_list[j]['car_name']))
plt.imshow(images_list[j]['car_orig_image'])
plt.subplot(n, 2, (cnt*2)+2)
plt.imshow(images_list[j]['car_orig_image'])
temp_image=images_list[j]['car_orig_image'].copy()
temp_image_upd = (temp_image / 255.).astype(np.float32)
temp_image_upd = cv2.resize(temp_image_upd, dsize = (128,128))
temp_image_upd_arr = np.empty((1,128,128,3))
temp_image_upd_arr[0]=temp_image_upd
# predict using the trained model
predicted_class = model_mobilenet.predict(temp_image_upd_arr)
# Index of the class with maximum probability
predicted_index = np.argmax(predicted_class[0])
predict_class=cars_dict[predicted_index+1]
# display actual image with predicted name
plt.title("Predicted: {}".format(predict_class))
predict_images(train_images_upd,3)
Showing images with index in : [8116 4088 7414] 1/1 [==============================] - 0s 19ms/step 1/1 [==============================] - 0s 17ms/step 1/1 [==============================] - 0s 18ms/step
As shown above, the MobileNet model has correctly predicted all the 3 random test samples. The classification model MobileNet can be saved for future use.
# Saving the model for future use
model_mobilenet.save('/content/drive/MyDrive/Project Files/model_mobilenet_classfier.h5')
model_mobilenet.save_weights('/content/drive/MyDrive/Project Files/model_mobilenet_classifier_weights.h5')
Object Detection models
Train and Test dataset preparation
IMAGE_HEIGHT = 224
IMAGE_WIDTH = 224
ALPHA = 1
from matplotlib import pyplot as plt
def show_images(images_list,n):
rand = np.random.randint(0, len(images_list), n) # Generating n random numbers from total number of images
print("Showing images with index in : {}".format(rand))
plt.figure(figsize=(10, 20))
for cnt,j in enumerate(rand):
plt.subplot(n, 2, (cnt*2)+1)
plt.title(images_list[j]['car_name'])
img_orig=images_list[j]['car_orig_image']
img_upd = cv2.resize(images_list[j]['car_orig_image'], dsize = (224,224))
plt.imshow(img_upd)
#plt.imshow(images_list[j]['car_orig_image'])
plt.subplot(n, 2, (cnt*2)+2)
#temp_image=images_list[j]['car_orig_image'].copy()
temp_image=img_upd.copy()
print(temp_image.shape)
(img_orig_h, img_orig_w, c)=img_orig.shape
gray = cv2.cvtColor(temp_image, cv2.COLOR_BGR2GRAY)
(x1, y1, x2, y2)=images_list[j]['bounding_box']
print((x1, y1, x2, y2))
x1_=int((x1/img_orig_w)*224)
x2_=int((x2/img_orig_w)*224)
y1_=int((y1/img_orig_h)*224)
y2_=int((y2/img_orig_h)*224)
print((x1_, y1_, x2_, y2_))
cv2.rectangle(temp_image, (x1_,y1_), (x2_,y2_), (255,0,0), 4)
plt.title(images_list[j]['car_name'])
plt.imshow(temp_image)
X = np.zeros((int(1000), IMAGE_HEIGHT, IMAGE_WIDTH , 3))
masks = np.zeros((int(1000), IMAGE_HEIGHT, IMAGE_WIDTH))
for index in range(0,1000):
img_orig = train_images_upd[index]['car_orig_image']
(img_orig_h, img_orig_w, c)=img_orig.shape
image_upd = cv2.resize(img_orig, dsize=(IMAGE_HEIGHT, IMAGE_WIDTH), interpolation=cv2.INTER_CUBIC)
X[index] = preprocess_input(np.array(image_upd, dtype=np.float32))
# for i in train_dataset[index][1]:
(x1, y1, x2, y2)=train_images_upd[index]['bounding_box']
x1_=int((x1/img_orig_w)*224)
x2_=int((x2/img_orig_w)*224)
y1_=int((y1/img_orig_h)*224)
y2_=int((y2/img_orig_h)*224)
# set all pixels within the mask co-ordinates to 1.
masks[index][y1_:y2_, x1_:x2_] = 1
X = np.array(X)
print("The shape of the features is {}".format(X.shape))
print("The shape of mask is {}".format(masks.shape))
The shape of the features is (1000, 224, 224, 3) The shape of mask is (1000, 224, 224)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, masks, test_size=0.2)
print("The shape of X_train is", X_train.shape)
print("The shape of y_train is", y_train.shape)
print("")
print("The shape of X_test is", X_test.shape)
print("The shape of y_test is", y_test.shape)
The shape of X_train is (800, 224, 224, 3) The shape of y_train is (800, 224, 224) The shape of X_test is (200, 224, 224, 3) The shape of y_test is (200, 224, 224)
import random
fig = plt.figure(figsize=(10,8))
n = random.randint(0, X_train.shape[0])
print("The random number is", n)
ax1 = fig.add_subplot(2, 2, 1)
ax1.imshow(X_train[n])
ax2 = fig.add_subplot(2, 2, 2)
ax2.imshow(y_train[n])
WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
The random number is 47
<matplotlib.image.AxesImage at 0x7f7b3af28790>
MobileNet model for car detection with mask
img_height=224
img_width=224
from tensorflow.keras.layers import Reshape, UpSampling2D, Concatenate, Conv2D
def create_cardetect_model(trainable=True):
cv_model = MobileNet(input_shape=(img_height, img_width, 3), include_top=False, alpha=1.0, weights="imagenet")
for layer in cv_model.layers:
layer.trainable = trainable
block0 = cv_model.get_layer("conv_pw_1_relu").output
block1 = cv_model.get_layer("conv_pw_3_relu").output
block2 = cv_model.get_layer("conv_pw_5_relu").output
block3 = cv_model.get_layer("conv_pw_11_relu").output
block4 = cv_model.get_layer("conv_pw_13_relu").output
x = Concatenate()([UpSampling2D()(block4), block3])
print(x.shape)
x = Concatenate()([UpSampling2D()(x), block2])
print(x.shape)
x = Concatenate()([UpSampling2D()(x), block1])
print(x.shape)
x = Concatenate()([UpSampling2D()(x), block0])
print(x.shape)
x = UpSampling2D()(x)
print(x.shape)
x = Conv2D(1, kernel_size=1, activation="sigmoid")(x)
x = Reshape((img_height, img_width))(x)
print(x.shape)
return Model(inputs=cv_model.input, outputs=x)
cardetect_model = create_cardetect_model(False)
(None, 14, 14, 1536) (None, 28, 28, 1792) (None, 56, 56, 1920) (None, 112, 112, 1984) (None, 224, 224, 1984) (None, 224, 224)
cardetect_model.summary()
Model: "model_5"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_6 (InputLayer) [(None, 224, 224, 3 0 []
)]
conv1 (Conv2D) (None, 112, 112, 32 864 ['input_6[0][0]']
)
conv1_bn (BatchNormalization) (None, 112, 112, 32 128 ['conv1[0][0]']
)
conv1_relu (ReLU) (None, 112, 112, 32 0 ['conv1_bn[0][0]']
)
conv_dw_1 (DepthwiseConv2D) (None, 112, 112, 32 288 ['conv1_relu[0][0]']
)
conv_dw_1_bn (BatchNormalizati (None, 112, 112, 32 128 ['conv_dw_1[0][0]']
on) )
conv_dw_1_relu (ReLU) (None, 112, 112, 32 0 ['conv_dw_1_bn[0][0]']
)
conv_pw_1 (Conv2D) (None, 112, 112, 64 2048 ['conv_dw_1_relu[0][0]']
)
conv_pw_1_bn (BatchNormalizati (None, 112, 112, 64 256 ['conv_pw_1[0][0]']
on) )
conv_pw_1_relu (ReLU) (None, 112, 112, 64 0 ['conv_pw_1_bn[0][0]']
)
conv_pad_2 (ZeroPadding2D) (None, 113, 113, 64 0 ['conv_pw_1_relu[0][0]']
)
conv_dw_2 (DepthwiseConv2D) (None, 56, 56, 64) 576 ['conv_pad_2[0][0]']
conv_dw_2_bn (BatchNormalizati (None, 56, 56, 64) 256 ['conv_dw_2[0][0]']
on)
conv_dw_2_relu (ReLU) (None, 56, 56, 64) 0 ['conv_dw_2_bn[0][0]']
conv_pw_2 (Conv2D) (None, 56, 56, 128) 8192 ['conv_dw_2_relu[0][0]']
conv_pw_2_bn (BatchNormalizati (None, 56, 56, 128) 512 ['conv_pw_2[0][0]']
on)
conv_pw_2_relu (ReLU) (None, 56, 56, 128) 0 ['conv_pw_2_bn[0][0]']
conv_dw_3 (DepthwiseConv2D) (None, 56, 56, 128) 1152 ['conv_pw_2_relu[0][0]']
conv_dw_3_bn (BatchNormalizati (None, 56, 56, 128) 512 ['conv_dw_3[0][0]']
on)
conv_dw_3_relu (ReLU) (None, 56, 56, 128) 0 ['conv_dw_3_bn[0][0]']
conv_pw_3 (Conv2D) (None, 56, 56, 128) 16384 ['conv_dw_3_relu[0][0]']
conv_pw_3_bn (BatchNormalizati (None, 56, 56, 128) 512 ['conv_pw_3[0][0]']
on)
conv_pw_3_relu (ReLU) (None, 56, 56, 128) 0 ['conv_pw_3_bn[0][0]']
conv_pad_4 (ZeroPadding2D) (None, 57, 57, 128) 0 ['conv_pw_3_relu[0][0]']
conv_dw_4 (DepthwiseConv2D) (None, 28, 28, 128) 1152 ['conv_pad_4[0][0]']
conv_dw_4_bn (BatchNormalizati (None, 28, 28, 128) 512 ['conv_dw_4[0][0]']
on)
conv_dw_4_relu (ReLU) (None, 28, 28, 128) 0 ['conv_dw_4_bn[0][0]']
conv_pw_4 (Conv2D) (None, 28, 28, 256) 32768 ['conv_dw_4_relu[0][0]']
conv_pw_4_bn (BatchNormalizati (None, 28, 28, 256) 1024 ['conv_pw_4[0][0]']
on)
conv_pw_4_relu (ReLU) (None, 28, 28, 256) 0 ['conv_pw_4_bn[0][0]']
conv_dw_5 (DepthwiseConv2D) (None, 28, 28, 256) 2304 ['conv_pw_4_relu[0][0]']
conv_dw_5_bn (BatchNormalizati (None, 28, 28, 256) 1024 ['conv_dw_5[0][0]']
on)
conv_dw_5_relu (ReLU) (None, 28, 28, 256) 0 ['conv_dw_5_bn[0][0]']
conv_pw_5 (Conv2D) (None, 28, 28, 256) 65536 ['conv_dw_5_relu[0][0]']
conv_pw_5_bn (BatchNormalizati (None, 28, 28, 256) 1024 ['conv_pw_5[0][0]']
on)
conv_pw_5_relu (ReLU) (None, 28, 28, 256) 0 ['conv_pw_5_bn[0][0]']
conv_pad_6 (ZeroPadding2D) (None, 29, 29, 256) 0 ['conv_pw_5_relu[0][0]']
conv_dw_6 (DepthwiseConv2D) (None, 14, 14, 256) 2304 ['conv_pad_6[0][0]']
conv_dw_6_bn (BatchNormalizati (None, 14, 14, 256) 1024 ['conv_dw_6[0][0]']
on)
conv_dw_6_relu (ReLU) (None, 14, 14, 256) 0 ['conv_dw_6_bn[0][0]']
conv_pw_6 (Conv2D) (None, 14, 14, 512) 131072 ['conv_dw_6_relu[0][0]']
conv_pw_6_bn (BatchNormalizati (None, 14, 14, 512) 2048 ['conv_pw_6[0][0]']
on)
conv_pw_6_relu (ReLU) (None, 14, 14, 512) 0 ['conv_pw_6_bn[0][0]']
conv_dw_7 (DepthwiseConv2D) (None, 14, 14, 512) 4608 ['conv_pw_6_relu[0][0]']
conv_dw_7_bn (BatchNormalizati (None, 14, 14, 512) 2048 ['conv_dw_7[0][0]']
on)
conv_dw_7_relu (ReLU) (None, 14, 14, 512) 0 ['conv_dw_7_bn[0][0]']
conv_pw_7 (Conv2D) (None, 14, 14, 512) 262144 ['conv_dw_7_relu[0][0]']
conv_pw_7_bn (BatchNormalizati (None, 14, 14, 512) 2048 ['conv_pw_7[0][0]']
on)
conv_pw_7_relu (ReLU) (None, 14, 14, 512) 0 ['conv_pw_7_bn[0][0]']
conv_dw_8 (DepthwiseConv2D) (None, 14, 14, 512) 4608 ['conv_pw_7_relu[0][0]']
conv_dw_8_bn (BatchNormalizati (None, 14, 14, 512) 2048 ['conv_dw_8[0][0]']
on)
conv_dw_8_relu (ReLU) (None, 14, 14, 512) 0 ['conv_dw_8_bn[0][0]']
conv_pw_8 (Conv2D) (None, 14, 14, 512) 262144 ['conv_dw_8_relu[0][0]']
conv_pw_8_bn (BatchNormalizati (None, 14, 14, 512) 2048 ['conv_pw_8[0][0]']
on)
conv_pw_8_relu (ReLU) (None, 14, 14, 512) 0 ['conv_pw_8_bn[0][0]']
conv_dw_9 (DepthwiseConv2D) (None, 14, 14, 512) 4608 ['conv_pw_8_relu[0][0]']
conv_dw_9_bn (BatchNormalizati (None, 14, 14, 512) 2048 ['conv_dw_9[0][0]']
on)
conv_dw_9_relu (ReLU) (None, 14, 14, 512) 0 ['conv_dw_9_bn[0][0]']
conv_pw_9 (Conv2D) (None, 14, 14, 512) 262144 ['conv_dw_9_relu[0][0]']
conv_pw_9_bn (BatchNormalizati (None, 14, 14, 512) 2048 ['conv_pw_9[0][0]']
on)
conv_pw_9_relu (ReLU) (None, 14, 14, 512) 0 ['conv_pw_9_bn[0][0]']
conv_dw_10 (DepthwiseConv2D) (None, 14, 14, 512) 4608 ['conv_pw_9_relu[0][0]']
conv_dw_10_bn (BatchNormalizat (None, 14, 14, 512) 2048 ['conv_dw_10[0][0]']
ion)
conv_dw_10_relu (ReLU) (None, 14, 14, 512) 0 ['conv_dw_10_bn[0][0]']
conv_pw_10 (Conv2D) (None, 14, 14, 512) 262144 ['conv_dw_10_relu[0][0]']
conv_pw_10_bn (BatchNormalizat (None, 14, 14, 512) 2048 ['conv_pw_10[0][0]']
ion)
conv_pw_10_relu (ReLU) (None, 14, 14, 512) 0 ['conv_pw_10_bn[0][0]']
conv_dw_11 (DepthwiseConv2D) (None, 14, 14, 512) 4608 ['conv_pw_10_relu[0][0]']
conv_dw_11_bn (BatchNormalizat (None, 14, 14, 512) 2048 ['conv_dw_11[0][0]']
ion)
conv_dw_11_relu (ReLU) (None, 14, 14, 512) 0 ['conv_dw_11_bn[0][0]']
conv_pw_11 (Conv2D) (None, 14, 14, 512) 262144 ['conv_dw_11_relu[0][0]']
conv_pw_11_bn (BatchNormalizat (None, 14, 14, 512) 2048 ['conv_pw_11[0][0]']
ion)
conv_pw_11_relu (ReLU) (None, 14, 14, 512) 0 ['conv_pw_11_bn[0][0]']
conv_pad_12 (ZeroPadding2D) (None, 15, 15, 512) 0 ['conv_pw_11_relu[0][0]']
conv_dw_12 (DepthwiseConv2D) (None, 7, 7, 512) 4608 ['conv_pad_12[0][0]']
conv_dw_12_bn (BatchNormalizat (None, 7, 7, 512) 2048 ['conv_dw_12[0][0]']
ion)
conv_dw_12_relu (ReLU) (None, 7, 7, 512) 0 ['conv_dw_12_bn[0][0]']
conv_pw_12 (Conv2D) (None, 7, 7, 1024) 524288 ['conv_dw_12_relu[0][0]']
conv_pw_12_bn (BatchNormalizat (None, 7, 7, 1024) 4096 ['conv_pw_12[0][0]']
ion)
conv_pw_12_relu (ReLU) (None, 7, 7, 1024) 0 ['conv_pw_12_bn[0][0]']
conv_dw_13 (DepthwiseConv2D) (None, 7, 7, 1024) 9216 ['conv_pw_12_relu[0][0]']
conv_dw_13_bn (BatchNormalizat (None, 7, 7, 1024) 4096 ['conv_dw_13[0][0]']
ion)
conv_dw_13_relu (ReLU) (None, 7, 7, 1024) 0 ['conv_dw_13_bn[0][0]']
conv_pw_13 (Conv2D) (None, 7, 7, 1024) 1048576 ['conv_dw_13_relu[0][0]']
conv_pw_13_bn (BatchNormalizat (None, 7, 7, 1024) 4096 ['conv_pw_13[0][0]']
ion)
conv_pw_13_relu (ReLU) (None, 7, 7, 1024) 0 ['conv_pw_13_bn[0][0]']
up_sampling2d_5 (UpSampling2D) (None, 14, 14, 1024 0 ['conv_pw_13_relu[0][0]']
)
concatenate_4 (Concatenate) (None, 14, 14, 1536 0 ['up_sampling2d_5[0][0]',
) 'conv_pw_11_relu[0][0]']
up_sampling2d_6 (UpSampling2D) (None, 28, 28, 1536 0 ['concatenate_4[0][0]']
)
concatenate_5 (Concatenate) (None, 28, 28, 1792 0 ['up_sampling2d_6[0][0]',
) 'conv_pw_5_relu[0][0]']
up_sampling2d_7 (UpSampling2D) (None, 56, 56, 1792 0 ['concatenate_5[0][0]']
)
concatenate_6 (Concatenate) (None, 56, 56, 1920 0 ['up_sampling2d_7[0][0]',
) 'conv_pw_3_relu[0][0]']
up_sampling2d_8 (UpSampling2D) (None, 112, 112, 19 0 ['concatenate_6[0][0]']
20)
concatenate_7 (Concatenate) (None, 112, 112, 19 0 ['up_sampling2d_8[0][0]',
84) 'conv_pw_1_relu[0][0]']
up_sampling2d_9 (UpSampling2D) (None, 224, 224, 19 0 ['concatenate_7[0][0]']
84)
conv2d_3 (Conv2D) (None, 224, 224, 1) 1985 ['up_sampling2d_9[0][0]']
reshape_1 (Reshape) (None, 224, 224) 0 ['conv2d_3[0][0]']
==================================================================================================
Total params: 3,230,849
Trainable params: 1,985
Non-trainable params: 3,228,864
__________________________________________________________________________________________________
from tensorflow import reduce_sum
from tensorflow.keras.backend import epsilon
# Defining dice coefficient
def dice_coefficient(y_true, y_pred):
numerator = 2 * reduce_sum(y_true * y_pred)
denominator = reduce_sum(y_true + y_pred)
return numerator / (denominator + epsilon())
from tensorflow.keras.losses import binary_crossentropy
from tensorflow.keras.backend import log
# Defining loss function
def loss(y_true, y_pred):
return binary_crossentropy(y_true, y_pred) - log(dice_coefficient(y_true, y_pred) + epsilon())
cardetect_model.compile(loss=loss, optimizer='adam', metrics=[dice_coefficient])
cardetect_model_fit=cardetect_model.fit(X_train, y_train, epochs=40, batch_size=1, callbacks=[reduce_lr, stop],validation_data=(X_test, y_test))
Epoch 1/40 800/800 [==============================] - 29s 34ms/step - loss: 0.5197 - dice_coefficient: 0.8397 - val_loss: 0.4166 - val_dice_coefficient: 0.8620 - lr: 0.0010 Epoch 2/40 800/800 [==============================] - 27s 33ms/step - loss: 0.4032 - dice_coefficient: 0.8710 - val_loss: 0.3850 - val_dice_coefficient: 0.8706 - lr: 0.0010 Epoch 3/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3772 - dice_coefficient: 0.8790 - val_loss: 0.4252 - val_dice_coefficient: 0.8673 - lr: 0.0010 Epoch 4/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3673 - dice_coefficient: 0.8834 - val_loss: 0.3710 - val_dice_coefficient: 0.8816 - lr: 0.0010 Epoch 5/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3625 - dice_coefficient: 0.8853 - val_loss: 0.3834 - val_dice_coefficient: 0.8777 - lr: 0.0010 Epoch 6/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3541 - dice_coefficient: 0.8882 - val_loss: 0.4237 - val_dice_coefficient: 0.8700 - lr: 0.0010 Epoch 7/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3581 - dice_coefficient: 0.8877 - val_loss: 0.3731 - val_dice_coefficient: 0.8815 - lr: 0.0010 Epoch 8/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3545 - dice_coefficient: 0.8882 - val_loss: 0.3666 - val_dice_coefficient: 0.8832 - lr: 0.0010 Epoch 9/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3515 - dice_coefficient: 0.8894 - val_loss: 0.3875 - val_dice_coefficient: 0.8806 - lr: 0.0010 Epoch 10/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3548 - dice_coefficient: 0.8901 - val_loss: 0.4114 - val_dice_coefficient: 0.8752 - lr: 0.0010 Epoch 11/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3527 - dice_coefficient: 0.8907 - val_loss: 0.3940 - val_dice_coefficient: 0.8759 - lr: 0.0010 Epoch 12/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3514 - dice_coefficient: 0.8907 - val_loss: 0.3670 - val_dice_coefficient: 0.8883 - lr: 0.0010 Epoch 13/40 800/800 [==============================] - 27s 33ms/step - loss: 0.3463 - dice_coefficient: 0.8919 - val_loss: 0.3678 - val_dice_coefficient: 0.8847 - lr: 0.0010 Epoch 14/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3500 - dice_coefficient: 0.8916 - val_loss: 0.4149 - val_dice_coefficient: 0.8744 - lr: 0.0010 Epoch 15/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3465 - dice_coefficient: 0.8921 - val_loss: 0.3656 - val_dice_coefficient: 0.8849 - lr: 0.0010 Epoch 16/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3462 - dice_coefficient: 0.8920 - val_loss: 0.4060 - val_dice_coefficient: 0.8761 - lr: 0.0010 Epoch 17/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3556 - dice_coefficient: 0.8913 - val_loss: 0.3794 - val_dice_coefficient: 0.8822 - lr: 0.0010 Epoch 18/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3430 - dice_coefficient: 0.8931 - val_loss: 0.3702 - val_dice_coefficient: 0.8858 - lr: 0.0010 Epoch 19/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3460 - dice_coefficient: 0.8928 - val_loss: 0.3783 - val_dice_coefficient: 0.8844 - lr: 0.0010 Epoch 20/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3457 - dice_coefficient: 0.8928 - val_loss: 0.3601 - val_dice_coefficient: 0.8868 - lr: 0.0010 Epoch 21/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3448 - dice_coefficient: 0.8926 - val_loss: 0.4753 - val_dice_coefficient: 0.8623 - lr: 0.0010 Epoch 22/40 800/800 [==============================] - 26s 33ms/step - loss: 0.3463 - dice_coefficient: 0.8935 - val_loss: 0.3789 - val_dice_coefficient: 0.8864 - lr: 0.0010 Epoch 23/40 799/800 [============================>.] - ETA: 0s - loss: 0.3468 - dice_coefficient: 0.8931 Epoch 23: ReduceLROnPlateau reducing learning rate to 0.00020000000949949026. 800/800 [==============================] - 26s 33ms/step - loss: 0.3468 - dice_coefficient: 0.8931 - val_loss: 0.3911 - val_dice_coefficient: 0.8802 - lr: 0.0010
## Plotting Train and Test loss
epoch=23
loss_train = cardetect_model_fit.history['loss']
loss_val = cardetect_model_fit.history['val_loss']
epochs = range(1,epoch+1)
plt.plot(epochs, loss_train, 'g', label='Training loss')
plt.plot(epochs, loss_val, 'b', label='validation loss')
plt.title('Training and Validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
## Plotting Train and Test accuracy
Acc_train = cardetect_model_fit.history['dice_coefficient']
Acc_val = cardetect_model_fit.history['val_dice_coefficient']
epochs = range(1,epoch+1)
plt.plot(epochs, Acc_train, 'g', label='Training accuracy')
plt.plot(epochs, Acc_val, 'b', label='validation accuracy')
plt.title('Training and Validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('accuracy')
plt.legend()
plt.show()
MobileNet has given good accuracies in both train set and test set. In train set, the model has given 89.31% dice_coefficient and in test set 88.02% dice_coefficient. Since the results are very close in train and test set, the model has consistently performed well and can be saved for future use.
# Saving the model for future use
cardetect_model.save('/content/drive/MyDrive/Project Files/model_mobilenet_cardetect.h5')
cardetect_model.save_weights('/content/drive/MyDrive/Project Files/model_mobilenet_cardetect_weights.h5')
Showing sample images and the predicted mask area.
def mobilenet_predict_show(n):
plt.figure(figsize=(10, 20))
for j in range(n):
plt.subplot(n, 2, (j*2)+1)
plt.title('Actual Image')
plt.imshow(X_test[j])
plt.subplot(n, 2, (j*2)+2)
plt.imshow(y_test[j])
plt.title("Predicted Mask on Image")
mobilenet_predict_show(5)
WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). WARNING:matplotlib.image:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
YOLO model for car detection with bounding box
!wget "https://pjreddie.com/media/files/yolov3.weights"
!wget "https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov3.cfg"
!wget "https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names"
--2023-01-14 18:47:23-- https://pjreddie.com/media/files/yolov3.weights Resolving pjreddie.com (pjreddie.com)... 128.208.4.108 Connecting to pjreddie.com (pjreddie.com)|128.208.4.108|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 248007048 (237M) [application/octet-stream] Saving to: ‘yolov3.weights’ yolov3.weights 100%[===================>] 236.52M 23.4MB/s in 11s 2023-01-14 18:47:35 (21.7 MB/s) - ‘yolov3.weights’ saved [248007048/248007048] --2023-01-14 18:47:35-- https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov3.cfg Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.111.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 8342 (8.1K) [text/plain] Saving to: ‘yolov3.cfg’ yolov3.cfg 100%[===================>] 8.15K --.-KB/s in 0s 2023-01-14 18:47:36 (92.6 MB/s) - ‘yolov3.cfg’ saved [8342/8342] --2023-01-14 18:47:36-- https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 625 [text/plain] Saving to: ‘coco.names’ coco.names 100%[===================>] 625 --.-KB/s in 0s 2023-01-14 18:47:37 (48.9 MB/s) - ‘coco.names’ saved [625/625]
### Load YOLO model with trained weights and coco classes
yolo_net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
classes = []
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
layer_names = yolo_net.getLayerNames()
#Get output layer names from the YOLO model
output_layers = [layer_names[i -1] for i in yolo_net.getUnconnectedOutLayers()]
print("Number of items loaded in output_layers :{}".format(len(output_layers)))
Number of items loaded in output_layers :3
import random
def yolo_model_fit(n):
yolo_img_predictions=[]
rand = random.sample(range(len(train_images_upd)), n)
for cnt,j in enumerate(rand):
samp_img=train_images_upd[j]['car_orig_image']
height, width, channels = samp_img.shape
blob = cv2.dnn.blobFromImage(samp_img, 1 / 255.0, (320, 320),swapRB=True, crop=False)
yolo_net.setInput(blob)
outs = yolo_net.forward(output_layers)
# detcting the objects in image
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.9:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
colors = np.random.uniform(0, 255, size=(len(classes), 3))
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
color = colors[class_ids[i]]
cv2.rectangle(samp_img, (x, y), (x + w, y + h),(255,0,0), 4)
cv2.putText(samp_img, label, (x, y -5),cv2.FONT_HERSHEY_SIMPLEX,1/2,color, 2)
yolo_img_predictions.append(samp_img)
return yolo_img_predictions
def yolo_predict_show(n):
yolo_img_predictions=yolo_model_fit(n)
plt.figure(figsize=(20, 20))
for i in range(len(yolo_img_predictions)):
plt.subplot(1, len(yolo_img_predictions), i+1)
plt.imshow(yolo_img_predictions[i])
plt.axis('off')
plt.show()
yolo_predict_show(4)
YOLO3 model has also produced good results. In the above plots, yolo model has correctly detected the cars in the random samples images.
### Function to save the tuned model as pickle file
import pickle
def save_tuned_model(model_name, file_name):
pickle.dump(model_name, open(file_name, 'wb'))
save_tuned_model(model_mobilenet,'/content/drive/MyDrive/Project Files/model_mobilenet_classfier.pkl')
WARNING:absl:Found untraced functions such as _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op while saving (showing 5 of 27). These functions will not be directly callable after loading.
save_tuned_model(cardetect_model,'/content/drive/MyDrive/Project Files/model_mobilenet_cardetect.pkl')
WARNING:absl:Found untraced functions such as _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op while saving (showing 5 of 28). These functions will not be directly callable after loading.
Models can also be saved as below
# Saving the model for future use
model_mobilenet.save('/content/drive/MyDrive/Project Files/model_mobilenet_classfier.h5')
model_mobilenet.save_weights('/content/drive/MyDrive/Project Files/model_mobilenet_classifier_weights.h5')
# Saving the model for future use
cardetect_model.save('/content/drive/MyDrive/Project Files/model_mobilenet_cardetect.h5')
cardetect_model.save_weights('/content/drive/MyDrive/Project Files/model_mobilenet_cardetect_weights.h5')
Of all the models tried above, MobileNet gave consistent and better predictions for both classification problem and object detection problem. Both models has given correct predictions for the random test samples tried. Hence the MobileNet models for both classification and object detection are saved for future use.
Final report is created separately as word document. The same will be uploaded to the Solution link.